Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTT/1 mux-demux for connections |
| 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> |
| 13 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 15 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 16 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 17 | #include <haproxy/h1.h> |
Willy Tarreau | c6fe884 | 2020-06-04 09:00:02 +0200 | [diff] [blame] | 18 | #include <haproxy/h1_htx.h> |
Willy Tarreau | bf07314 | 2020-06-03 12:04:01 +0200 | [diff] [blame] | 19 | #include <haproxy/h2.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 21 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/istbuf.h> |
| 23 | #include <haproxy/log.h> |
Willy Tarreau | 551271d | 2020-06-04 08:32:23 +0200 | [diff] [blame] | 24 | #include <haproxy/pipe-t.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 25 | #include <haproxy/proxy-t.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 26 | #include <haproxy/session-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 27 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 28 | #include <haproxy/stream_interface.h> |
Willy Tarreau | c6d61d7 | 2020-06-04 19:02:42 +0200 | [diff] [blame] | 29 | #include <haproxy/trace.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * H1 Connection flags (32 bits) |
| 33 | */ |
| 34 | #define H1C_F_NONE 0x00000000 |
| 35 | |
| 36 | /* Flags indicating why writing output data are blocked */ |
| 37 | #define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */ |
| 38 | #define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */ |
| 39 | /* 0x00000004 - 0x00000008 unused */ |
| 40 | |
| 41 | /* Flags indicating why reading input data are blocked. */ |
| 42 | #define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */ |
| 43 | #define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */ |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 44 | #define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */ |
| 45 | /* 0x00000080 - 0x00000800 unused */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 46 | |
Christopher Faulet | 7b109f2 | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 47 | /* Flags indicating the connection state */ |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 48 | #define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred (conn-stream may still be attached) */ |
| 49 | #define H1C_F_CS_SHUTDOWN 0x00002000 /* connection must be shut down ASAP flushing output first (conn-stream may still be attached) */ |
| 50 | /* 0x00000040 unused */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 51 | #define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused |
| 52 | * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 53 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 54 | #define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */ |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 55 | #define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 56 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 57 | #define H1C_F_CO_MSG_MORE 0x00040000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */ |
| 58 | #define H1C_F_CO_STREAMER 0x00080000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */ |
| 59 | |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 60 | #define H1C_F_WAIT_OPPOSITE 0x00100000 /* Don't read more data for now, waiting sync with opposite side */ |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 61 | #define H1C_F_WANT_SPLICE 0x00200000 /* Don't read into a bufffer because we want to use or we are using splicing */ |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 62 | #define H1C_F_IS_BACK 0x00400000 /* Set on outgoing connection */ |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 63 | |
| 64 | #define H1C_F_CS_EMBRYONIC 0x01000000 /* Set when a H1 stream with no conn-stream is attached to the connection */ |
| 65 | #define H1C_F_CS_ATTACHED 0x02000000 /* Set when a H1 stream with a conn-stream is attached to the connection */ |
| 66 | #define H1C_F_CS_ALIVE (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 67 | /* |
| 68 | * H1 Stream flags (32 bits) |
| 69 | */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 70 | #define H1S_F_NONE 0x00000000 |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 71 | /* 0x00000001..0x00000004 unsued */ |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 72 | #define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 73 | #define H1S_F_WANT_KAL 0x00000010 |
| 74 | #define H1S_F_WANT_TUN 0x00000020 |
| 75 | #define H1S_F_WANT_CLO 0x00000040 |
| 76 | #define H1S_F_WANT_MSK 0x00000070 |
| 77 | #define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */ |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 78 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 79 | #define H1S_F_PARSING_DONE 0x00000400 /* Set when incoming message parsing is finished (EOM added) */ |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 80 | #define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */ |
| 81 | #define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */ |
| 82 | #define H1S_F_ERROR 0x00001800 /* stream error mask */ |
| 83 | |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 84 | #define H1S_F_HAVE_SRV_NAME 0x00002000 /* Set during output process if the server name header was added to the request */ |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 85 | #define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 86 | |
| 87 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 88 | struct h1c { |
| 89 | struct connection *conn; |
| 90 | struct proxy *px; |
| 91 | uint32_t flags; /* Connection flags: H1C_F_* */ |
| 92 | |
| 93 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 94 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
| 95 | |
| 96 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 97 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 98 | |
| 99 | struct h1s *h1s; /* H1 stream descriptor */ |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 100 | struct task *task; /* timeout management task */ |
| 101 | int timeout; /* idle timeout duration in ticks */ |
| 102 | int shut_timeout; /* idle timeout duration in ticks after stream shutdown */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* H1 stream descriptor */ |
| 106 | struct h1s { |
| 107 | struct h1c *h1c; |
| 108 | struct conn_stream *cs; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 109 | uint32_t flags; /* Connection flags: H1S_F_* */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 110 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 111 | struct wait_event *subs; /* Address of the wait_event the conn_stream associated is waiting on */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 112 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 113 | struct session *sess; /* Associated session */ |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 114 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 115 | struct h1m req; |
| 116 | struct h1m res; |
| 117 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 118 | enum http_meth_t meth; /* HTTP request method */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 119 | uint16_t status; /* HTTP response status */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 122 | /* Map of headers used to convert outgoing headers */ |
| 123 | struct h1_hdrs_map { |
| 124 | char *name; |
| 125 | struct eb_root map; |
| 126 | }; |
| 127 | |
| 128 | /* An entry in a headers map */ |
| 129 | struct h1_hdr_entry { |
| 130 | struct ist name; |
| 131 | struct ebpt_node node; |
| 132 | }; |
| 133 | |
| 134 | /* Declare the headers map */ |
| 135 | static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT }; |
| 136 | |
| 137 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 138 | /* trace source and events */ |
| 139 | static void h1_trace(enum trace_level level, uint64_t mask, |
| 140 | const struct trace_source *src, |
| 141 | const struct ist where, const struct ist func, |
| 142 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 143 | |
| 144 | /* The event representation is split like this : |
| 145 | * h1c - internal H1 connection |
| 146 | * h1s - internal H1 stream |
| 147 | * strm - application layer |
| 148 | * rx - data receipt |
| 149 | * tx - data transmission |
| 150 | * |
| 151 | */ |
| 152 | static const struct trace_event h1_trace_events[] = { |
| 153 | #define H1_EV_H1C_NEW (1ULL << 0) |
| 154 | { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" }, |
| 155 | #define H1_EV_H1C_RECV (1ULL << 1) |
| 156 | { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" }, |
| 157 | #define H1_EV_H1C_SEND (1ULL << 2) |
| 158 | { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" }, |
| 159 | #define H1_EV_H1C_BLK (1ULL << 3) |
| 160 | { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" }, |
| 161 | #define H1_EV_H1C_WAKE (1ULL << 4) |
| 162 | { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" }, |
| 163 | #define H1_EV_H1C_END (1ULL << 5) |
| 164 | { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" }, |
| 165 | #define H1_EV_H1C_ERR (1ULL << 6) |
| 166 | { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" }, |
| 167 | |
| 168 | #define H1_EV_RX_DATA (1ULL << 7) |
| 169 | { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" }, |
| 170 | #define H1_EV_RX_EOI (1ULL << 8) |
| 171 | { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" }, |
| 172 | #define H1_EV_RX_HDRS (1ULL << 9) |
| 173 | { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" }, |
| 174 | #define H1_EV_RX_BODY (1ULL << 10) |
| 175 | { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" }, |
| 176 | #define H1_EV_RX_TLRS (1ULL << 11) |
| 177 | { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" }, |
| 178 | |
| 179 | #define H1_EV_TX_DATA (1ULL << 12) |
| 180 | { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" }, |
| 181 | #define H1_EV_TX_EOI (1ULL << 13) |
| 182 | { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" }, |
| 183 | #define H1_EV_TX_HDRS (1ULL << 14) |
| 184 | { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" }, |
| 185 | #define H1_EV_TX_BODY (1ULL << 15) |
| 186 | { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" }, |
| 187 | #define H1_EV_TX_TLRS (1ULL << 16) |
| 188 | { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" }, |
| 189 | |
| 190 | #define H1_EV_H1S_NEW (1ULL << 17) |
| 191 | { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" }, |
| 192 | #define H1_EV_H1S_BLK (1ULL << 18) |
| 193 | { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" }, |
| 194 | #define H1_EV_H1S_END (1ULL << 19) |
| 195 | { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" }, |
| 196 | #define H1_EV_H1S_ERR (1ULL << 20) |
| 197 | { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" }, |
| 198 | |
| 199 | #define H1_EV_STRM_NEW (1ULL << 21) |
| 200 | { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 201 | #define H1_EV_STRM_RECV (1ULL << 22) |
| 202 | { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 203 | #define H1_EV_STRM_SEND (1ULL << 23) |
| 204 | { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 205 | #define H1_EV_STRM_WAKE (1ULL << 24) |
| 206 | { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" }, |
| 207 | #define H1_EV_STRM_SHUT (1ULL << 25) |
| 208 | { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 209 | #define H1_EV_STRM_END (1ULL << 26) |
| 210 | { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 211 | #define H1_EV_STRM_ERR (1ULL << 27) |
| 212 | { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 213 | |
| 214 | { } |
| 215 | }; |
| 216 | |
| 217 | static const struct name_desc h1_trace_lockon_args[4] = { |
| 218 | /* arg1 */ { /* already used by the connection */ }, |
| 219 | /* arg2 */ { .name="h1s", .desc="H1 stream" }, |
| 220 | /* arg3 */ { }, |
| 221 | /* arg4 */ { } |
| 222 | }; |
| 223 | |
| 224 | static const struct name_desc h1_trace_decoding[] = { |
| 225 | #define H1_VERB_CLEAN 1 |
| 226 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 227 | #define H1_VERB_MINIMAL 2 |
| 228 | { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" }, |
| 229 | #define H1_VERB_SIMPLE 3 |
| 230 | { .name="simple", .desc="add request/response status line or htx info when available" }, |
| 231 | #define H1_VERB_ADVANCED 4 |
| 232 | { .name="advanced", .desc="add header fields or frame decoding when available" }, |
| 233 | #define H1_VERB_COMPLETE 5 |
| 234 | { .name="complete", .desc="add full data dump when available" }, |
| 235 | { /* end */ } |
| 236 | }; |
| 237 | |
| 238 | static struct trace_source trace_h1 = { |
| 239 | .name = IST("h1"), |
| 240 | .desc = "HTTP/1 multiplexer", |
| 241 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 242 | .default_cb = h1_trace, |
| 243 | .known_events = h1_trace_events, |
| 244 | .lockon_args = h1_trace_lockon_args, |
| 245 | .decoding = h1_trace_decoding, |
| 246 | .report_events = ~0, // report everything by default |
| 247 | }; |
| 248 | |
| 249 | #define TRACE_SOURCE &trace_h1 |
| 250 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 251 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 252 | /* the h1c and h1s pools */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 253 | DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c)); |
| 254 | DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 255 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 256 | static int h1_recv(struct h1c *h1c); |
| 257 | static int h1_send(struct h1c *h1c); |
| 258 | static int h1_process(struct h1c *h1c); |
| 259 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 260 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state); |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 261 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode); |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 262 | static void h1_wake_stream_for_recv(struct h1s *h1s); |
| 263 | static void h1_wake_stream_for_send(struct h1s *h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 264 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 265 | /* the H1 traces always expect that arg1, if non-null, is of type connection |
| 266 | * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and |
| 267 | * that arg3, if non-null, is a htx for rx/tx headers. |
| 268 | */ |
| 269 | static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 270 | const struct ist where, const struct ist func, |
| 271 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 272 | { |
| 273 | const struct connection *conn = a1; |
| 274 | const struct h1c *h1c = conn ? conn->ctx : NULL; |
| 275 | const struct h1s *h1s = a2; |
| 276 | const struct htx *htx = a3; |
| 277 | const size_t *val = a4; |
| 278 | |
| 279 | if (!h1c) |
| 280 | h1c = (h1s ? h1s->h1c : NULL); |
| 281 | |
| 282 | if (!h1c || src->verbosity < H1_VERB_CLEAN) |
| 283 | return; |
| 284 | |
| 285 | /* Display frontend/backend info by default */ |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 286 | chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F')); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 287 | |
| 288 | /* Display request and response states if h1s is defined */ |
| 289 | if (h1s) |
| 290 | chunk_appendf(&trace_buf, " [%s, %s]", |
| 291 | h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state)); |
| 292 | |
| 293 | if (src->verbosity == H1_VERB_CLEAN) |
| 294 | return; |
| 295 | |
| 296 | /* Display the value to the 4th argument (level > STATE) */ |
| 297 | if (src->level > TRACE_LEVEL_STATE && val) |
Willy Tarreau | e18f53e | 2019-11-27 15:41:31 +0100 | [diff] [blame] | 298 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 299 | |
| 300 | /* Display status-line if possible (verbosity > MINIMAL) */ |
| 301 | if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) { |
| 302 | const struct htx_blk *blk = htx_get_head_blk(htx); |
| 303 | const struct htx_sl *sl = htx_get_blk_ptr(htx, blk); |
| 304 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 305 | |
| 306 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) |
| 307 | chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"", |
| 308 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 309 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 310 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
| 311 | } |
| 312 | |
| 313 | /* Display h1c info and, if defined, h1s info (pointer + flags) */ |
| 314 | chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags); |
| 315 | if (h1s) |
| 316 | chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags); |
| 317 | |
| 318 | if (src->verbosity == H1_VERB_MINIMAL) |
| 319 | return; |
| 320 | |
| 321 | /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */ |
| 322 | if (src->level > TRACE_LEVEL_USER) { |
| 323 | if (src->verbosity == H1_VERB_COMPLETE || |
| 324 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV)))) |
| 325 | chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u", |
| 326 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 327 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf)); |
| 328 | if (src->verbosity == H1_VERB_COMPLETE || |
| 329 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND)))) |
| 330 | chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u", |
| 331 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 332 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 333 | } |
| 334 | |
| 335 | /* Display htx info if defined (level > USER) */ |
| 336 | if (src->level > TRACE_LEVEL_USER && htx) { |
| 337 | int full = 0; |
| 338 | |
| 339 | /* Full htx info (level > STATE && verbosity > SIMPLE) */ |
| 340 | if (src->level > TRACE_LEVEL_STATE) { |
| 341 | if (src->verbosity == H1_VERB_COMPLETE) |
| 342 | full = 1; |
| 343 | else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS))) |
| 344 | full = 1; |
| 345 | } |
| 346 | |
| 347 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 348 | htx_dump(&trace_buf, htx, full); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 353 | /*****************************************************/ |
| 354 | /* functions below are for dynamic buffer management */ |
| 355 | /*****************************************************/ |
| 356 | /* |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 357 | * Indicates whether or not we may receive data. The rules are the following : |
| 358 | * - if an error or a shutdown for reads was detected on the connection we |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 359 | * must not attempt to receive |
| 360 | * - if we are waiting for the connection establishment, we must not attempt |
| 361 | * to receive |
| 362 | * - if an error was detected on the stream we must not attempt to receive |
| 363 | * - if reads are explicitly disabled, we must not attempt to receive |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 364 | * - if the input buffer failed to be allocated or is full , we must not try |
| 365 | * to receive |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 366 | * - if the mux is not blocked on an input condition, we may attempt to receive |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 367 | * - otherwise must may not attempt to receive |
| 368 | */ |
| 369 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 370 | { |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 371 | if (h1c->flags & H1C_F_CS_ERROR) { |
| 372 | TRACE_DEVEL("recv not allowed because of 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] | 373 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 374 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 375 | |
Willy Tarreau | 2febb84 | 2020-07-31 09:15:43 +0200 | [diff] [blame] | 376 | if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 377 | TRACE_DEVEL("recv not allowed because of (error|read0|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] | 378 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 379 | } |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 380 | |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 381 | if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) { |
| 382 | TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
| 383 | return 0; |
| 384 | } |
| 385 | |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 386 | if (h1c->flags & H1C_F_WAIT_OPPOSITE) { |
| 387 | TRACE_DEVEL("recv not allowed (wait_opposite)", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Willy Tarreau | f5ea3a8 | 2020-07-31 09:16:23 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 391 | if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 392 | return 1; |
| 393 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 394 | TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 400 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 401 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 402 | * impossible to wake up and we prefer to be woken up later. |
| 403 | */ |
| 404 | static int h1_buf_available(void *target) |
| 405 | { |
| 406 | struct h1c *h1c = target; |
| 407 | |
| 408 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 409 | 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] | 410 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 411 | if (h1_recv_allowed(h1c)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 412 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 417 | 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] | 418 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 419 | if (h1c->h1s) |
| 420 | h1_wake_stream_for_send(h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 421 | return 1; |
| 422 | } |
| 423 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 424 | if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc_margin(&h1c->h1s->rxbuf, 0)) { |
| 425 | TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn); |
| 426 | h1c->flags &= ~H1C_F_IN_SALLOC; |
| 427 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 428 | return 1; |
| 429 | } |
| 430 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 436 | */ |
| 437 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 438 | { |
| 439 | struct buffer *buf = NULL; |
| 440 | |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 441 | if (likely(!MT_LIST_ADDED(&h1c->buf_wait.list)) && |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 442 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 443 | h1c->buf_wait.target = h1c; |
| 444 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
Willy Tarreau | 8689127 | 2020-07-10 08:22:26 +0200 | [diff] [blame] | 445 | MT_LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 446 | } |
| 447 | return buf; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 452 | * wait queue. |
| 453 | */ |
| 454 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 455 | { |
| 456 | if (bptr->size) { |
| 457 | b_free(bptr); |
| 458 | offer_buffers(h1c->buf_wait.target, tasks_run_queue); |
| 459 | } |
| 460 | } |
| 461 | |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 462 | /* returns the number of streams in use on a connection to figure if it's idle |
| 463 | * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or |
| 464 | * not. This flag is only set when no H1S is attached and when the previous |
| 465 | * stream, if any, was fully terminated without any error and in K/A mode. |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 466 | */ |
| 467 | static int h1_used_streams(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 468 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 469 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 470 | |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 471 | return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 474 | /* returns the number of streams still available on a connection */ |
| 475 | static int h1_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 476 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 477 | return 1 - h1_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 478 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 480 | /* Refresh the h1c task timeout if necessary */ |
| 481 | static void h1_refresh_timeout(struct h1c *h1c) |
| 482 | { |
| 483 | if (h1c->task) { |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame^] | 484 | if (!(h1c->flags & H1C_F_CS_ALIVE) || (h1c->flags & H1C_F_CS_SHUTDOWN)) { |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 485 | /* half-closed or dead connections : switch to clientfin/serverfin |
| 486 | * timeouts so that we don't hang too long on clients that have |
| 487 | * gone away (especially in tunnel mode). |
Willy Tarreau | 4313d5a | 2020-09-08 15:40:57 +0200 | [diff] [blame] | 488 | */ |
| 489 | h1c->task->expire = tick_add(now_ms, h1c->shut_timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame^] | 490 | TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 491 | } |
| 492 | else if (b_data(&h1c->obuf)) { |
| 493 | /* connection with pending outgoing data, need a timeout (server or client). */ |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 494 | h1c->task->expire = tick_add(now_ms, h1c->timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame^] | 495 | TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 496 | } |
| 497 | else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & (H1C_F_CS_IDLE|H1C_F_CS_EMBRYONIC))) { |
Christopher Faulet | 268c92e | 2020-12-01 11:42:53 +0100 | [diff] [blame] | 498 | /* front connections waiting for a stream need a timeout. client timeout by |
| 499 | * default but http-keep-alive if defined |
| 500 | */ |
| 501 | int timeout = h1c->timeout; |
| 502 | |
| 503 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 504 | timeout = tick_first(timeout, h1c->px->timeout.httpka); |
| 505 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 506 | h1c->task->expire = tick_add(now_ms, timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame^] | 507 | TRACE_DEVEL("refreshing connection's timeout (alive front h1c without a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 508 | } |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame^] | 509 | else { |
| 510 | /* alive back connections of front connections with a conn-stream attached */ |
| 511 | h1c->task->expire = TICK_ETERNITY; |
| 512 | TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 513 | } |
| 514 | |
| 515 | TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire}); |
| 516 | task_queue(h1c->task); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 517 | } |
| 518 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 519 | /*****************************************************************/ |
| 520 | /* functions below are dedicated to the mux setup and management */ |
| 521 | /*****************************************************************/ |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 522 | |
| 523 | /* returns non-zero if there are input data pending for stream h1s. */ |
| 524 | static inline size_t h1s_data_pending(const struct h1s *h1s) |
| 525 | { |
| 526 | const struct h1m *h1m; |
| 527 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 528 | h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 529 | if (h1m->state == H1_MSG_DONE) |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 530 | return !(h1s->flags & H1S_F_PARSING_DONE); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 531 | |
| 532 | return b_data(&h1s->h1c->ibuf); |
| 533 | } |
| 534 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 535 | static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 536 | { |
| 537 | struct conn_stream *cs; |
| 538 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 539 | TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 540 | cs = cs_new(h1s->h1c->conn, h1s->h1c->conn->target); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 541 | if (!cs) { |
| 542 | TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 543 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 544 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 545 | h1s->cs = cs; |
| 546 | cs->ctx = h1s; |
| 547 | |
| 548 | if (h1s->flags & H1S_F_NOT_FIRST) |
| 549 | cs->flags |= CS_FL_NOT_FIRST; |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 550 | h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 551 | |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 552 | if (global.tune.options & GTUNE_USE_SPLICE) { |
| 553 | TRACE_STATE("notify the mux can use splicing", H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 554 | cs->flags |= CS_FL_MAY_SPLICE; |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 555 | } |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 556 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 557 | if (stream_create_from_cs(cs, input) < 0) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 558 | TRACE_DEVEL("leaving on stream creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 559 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 560 | } |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 561 | *input = BUF_NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 562 | |
| 563 | TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 564 | return cs; |
| 565 | |
| 566 | err: |
| 567 | cs_free(cs); |
| 568 | h1s->cs = NULL; |
| 569 | return NULL; |
| 570 | } |
| 571 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 572 | static struct h1s *h1s_new(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 573 | { |
| 574 | struct h1s *h1s; |
| 575 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 576 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 577 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 578 | h1s = pool_alloc(pool_head_h1s); |
| 579 | if (!h1s) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 580 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 581 | h1s->h1c = h1c; |
| 582 | h1c->h1s = h1s; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 583 | h1s->sess = NULL; |
| 584 | h1s->cs = NULL; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 585 | h1s->flags = H1S_F_WANT_KAL; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 586 | h1s->subs = NULL; |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 587 | h1s->rxbuf = BUF_NULL; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 588 | |
| 589 | h1m_init_req(&h1s->req); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 590 | h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 591 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 592 | h1m_init_res(&h1s->res); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 593 | h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 594 | |
| 595 | h1s->status = 0; |
| 596 | h1s->meth = HTTP_METH_OTHER; |
| 597 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 598 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 599 | h1s->flags |= H1S_F_NOT_FIRST; |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 600 | h1c->flags = (h1c->flags & ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_CS_EMBRYONIC; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 601 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 602 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 603 | return h1s; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 604 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 605 | fail: |
| 606 | TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn); |
| 607 | return NULL; |
| 608 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 609 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 610 | static struct h1s *h1c_frt_stream_new(struct h1c *h1c) |
| 611 | { |
| 612 | struct session *sess = h1c->conn->owner; |
| 613 | struct h1s *h1s; |
| 614 | |
| 615 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 616 | |
| 617 | h1s = h1s_new(h1c); |
| 618 | if (!h1s) |
| 619 | goto fail; |
| 620 | |
| 621 | h1s->sess = sess; |
| 622 | |
| 623 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 624 | h1s->req.err_pos = -1; |
| 625 | |
| 626 | if (!h1s_new_cs(h1s, &BUF_NULL)) |
| 627 | goto fail_cs; |
| 628 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 629 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 630 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 631 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 632 | fail_cs: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 633 | pool_free(pool_head_h1s, h1s); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 634 | fail: |
| 635 | sess_log(sess); |
| 636 | TRACE_DEVEL("leaving in error", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn); |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct conn_stream *cs, struct session *sess) |
| 641 | { |
| 642 | struct h1s *h1s; |
| 643 | |
| 644 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 645 | |
| 646 | h1s = h1s_new(h1c); |
| 647 | if (!h1s) |
| 648 | goto fail; |
| 649 | |
| 650 | h1s->cs = cs; |
| 651 | h1s->sess = sess; |
| 652 | cs->ctx = h1s; |
| 653 | |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 654 | h1c->flags = (h1c->flags & ~H1C_F_CS_EMBRYONIC) | H1C_F_CS_ATTACHED; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 655 | |
| 656 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 657 | h1s->res.err_pos = -1; |
| 658 | |
| 659 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 660 | return h1s; |
| 661 | |
| 662 | fail: |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 663 | TRACE_DEVEL("leaving in error", 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] | 664 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void h1s_destroy(struct h1s *h1s) |
| 668 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 669 | if (h1s) { |
| 670 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 671 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 672 | TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 673 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 674 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 675 | if (h1s->subs) |
| 676 | h1s->subs->events = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 677 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 678 | h1_release_buf(h1c, &h1s->rxbuf); |
| 679 | |
Christopher Faulet | 295b8d1 | 2020-09-30 17:14:55 +0200 | [diff] [blame] | 680 | h1c->flags &= ~(H1C_F_WAIT_OPPOSITE|H1C_F_WANT_SPLICE|H1C_F_CS_EMBRYONIC|H1C_F_CS_ATTACHED| |
| 681 | H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC| |
| 682 | H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 683 | if (h1s->flags & H1S_F_ERROR) { |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 684 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 685 | TRACE_STATE("h1s on error, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s); |
| 686 | } |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 687 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 688 | if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 689 | !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */ |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 690 | (h1s->flags & (H1S_F_WANT_KAL|H1S_F_PARSING_DONE)) == (H1S_F_WANT_KAL|H1S_F_PARSING_DONE) && /* K/A possible */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 691 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */ |
| 692 | h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ); |
| 693 | TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s); |
| 694 | } |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 695 | else { |
| 696 | TRACE_STATE("set shudown on h1c", H1_EV_H1C_ERR, h1c->conn, h1s); |
| 697 | h1c->flags |= H1C_F_CS_SHUTDOWN; |
| 698 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 699 | pool_free(pool_head_h1s, h1s); |
| 700 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 704 | * Initialize the mux once it's attached. It is expected that conn->ctx points |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 705 | * to the existing conn_stream (for outgoing connections or for incoming ones |
| 706 | * during a mux upgrade) or NULL (for incoming ones during the connection |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 707 | * establishment). <input> is always used as Input buffer and may contain |
| 708 | * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on |
| 709 | * error. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 710 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 711 | static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess, |
| 712 | struct buffer *input) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 713 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 714 | struct h1c *h1c; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 715 | struct task *t = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 716 | void *conn_ctx = conn->ctx; |
| 717 | |
| 718 | TRACE_ENTER(H1_EV_H1C_NEW); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 719 | |
| 720 | h1c = pool_alloc(pool_head_h1c); |
| 721 | if (!h1c) |
| 722 | goto fail_h1c; |
| 723 | h1c->conn = conn; |
| 724 | h1c->px = proxy; |
| 725 | |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 726 | h1c->flags = H1C_F_CS_IDLE; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 727 | h1c->ibuf = *input; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 728 | h1c->obuf = BUF_NULL; |
| 729 | h1c->h1s = NULL; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 730 | h1c->task = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 731 | |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 732 | MT_LIST_INIT(&h1c->buf_wait.list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 733 | h1c->wait_event.tasklet = tasklet_new(); |
| 734 | if (!h1c->wait_event.tasklet) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 735 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 736 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 737 | h1c->wait_event.tasklet->context = h1c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 738 | h1c->wait_event.events = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 739 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 740 | if (conn_is_back(conn)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 741 | h1c->flags |= (H1C_F_IS_BACK|H1C_F_WAIT_OPPOSITE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 742 | h1c->shut_timeout = h1c->timeout = proxy->timeout.server; |
| 743 | if (tick_isset(proxy->timeout.serverfin)) |
| 744 | h1c->shut_timeout = proxy->timeout.serverfin; |
| 745 | } else { |
| 746 | h1c->shut_timeout = h1c->timeout = proxy->timeout.client; |
| 747 | if (tick_isset(proxy->timeout.clientfin)) |
| 748 | h1c->shut_timeout = proxy->timeout.clientfin; |
| 749 | } |
| 750 | if (tick_isset(h1c->timeout)) { |
| 751 | t = task_new(tid_bit); |
| 752 | if (!t) |
| 753 | goto fail; |
| 754 | |
| 755 | h1c->task = t; |
| 756 | t->process = h1_timeout_task; |
| 757 | t->context = h1c; |
| 758 | t->expire = tick_add(now_ms, h1c->timeout); |
| 759 | } |
| 760 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 761 | conn->ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 762 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 763 | /* Always Create a new H1S */ |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 764 | if (!(h1c->flags & H1C_F_IS_BACK)) { |
| 765 | if (!h1c_frt_stream_new(h1c)) |
| 766 | goto fail; |
| 767 | } |
| 768 | else { |
| 769 | if (!h1c_bck_stream_new(h1c, conn_ctx, sess)) |
| 770 | goto fail; |
| 771 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 772 | |
| 773 | if (t) |
| 774 | task_queue(t); |
| 775 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 776 | /* Try to read, if nothing is available yet we'll just subscribe */ |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 777 | if (!h1_recv_allowed(h1c)) |
| 778 | 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] | 779 | |
| 780 | /* mux->wake will be called soon to complete the operation */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 781 | TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 782 | return 0; |
| 783 | |
| 784 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 785 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 786 | if (h1c->wait_event.tasklet) |
| 787 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 788 | pool_free(pool_head_h1c, h1c); |
| 789 | fail_h1c: |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 790 | conn->ctx = conn_ctx; // restore saved context |
| 791 | 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] | 792 | return -1; |
| 793 | } |
| 794 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 795 | /* release function. This one should be called to free all resources allocated |
| 796 | * to the mux. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 797 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 798 | static void h1_release(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 799 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 800 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 801 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 802 | TRACE_POINT(H1_EV_H1C_END); |
| 803 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 804 | if (h1c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 805 | /* The connection must be aattached to this mux to be released */ |
| 806 | if (h1c->conn && h1c->conn->ctx == h1c) |
| 807 | conn = h1c->conn; |
| 808 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 809 | TRACE_DEVEL("freeing h1c", H1_EV_H1C_END, conn); |
| 810 | |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 811 | if (conn && h1c->flags & H1C_F_UPG_H2C) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 812 | TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn); |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 813 | h1c->flags &= ~H1C_F_UPG_H2C; |
Olivier Houchard | 2ab3dad | 2019-07-03 13:08:18 +0200 | [diff] [blame] | 814 | /* Make sure we're no longer subscribed to anything */ |
| 815 | if (h1c->wait_event.events) |
| 816 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 817 | h1c->wait_event.events, &h1c->wait_event); |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 818 | if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 819 | /* connection successfully upgraded to H2, this |
| 820 | * mux was already released */ |
| 821 | return; |
| 822 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 823 | TRACE_DEVEL("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn); |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 824 | sess_log(conn->owner); /* Log if the upgrade failed */ |
| 825 | } |
| 826 | |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 827 | |
Willy Tarreau | 2104659 | 2020-02-26 10:39:36 +0100 | [diff] [blame] | 828 | if (MT_LIST_ADDED(&h1c->buf_wait.list)) |
| 829 | MT_LIST_DEL(&h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 830 | |
| 831 | h1_release_buf(h1c, &h1c->ibuf); |
| 832 | h1_release_buf(h1c, &h1c->obuf); |
| 833 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 834 | if (h1c->task) { |
| 835 | h1c->task->context = NULL; |
| 836 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 837 | h1c->task = NULL; |
| 838 | } |
| 839 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 840 | if (h1c->wait_event.tasklet) |
| 841 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 842 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 843 | h1s_destroy(h1c->h1s); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 844 | if (conn && h1c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 845 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 846 | &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 847 | pool_free(pool_head_h1c, h1c); |
| 848 | } |
| 849 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 850 | if (conn) { |
| 851 | conn->mux = NULL; |
| 852 | conn->ctx = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 853 | TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 854 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 855 | conn_stop_tracking(conn); |
| 856 | conn_full_close(conn); |
| 857 | if (conn->destroy_cb) |
| 858 | conn->destroy_cb(conn); |
| 859 | conn_free(conn); |
| 860 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /******************************************************/ |
| 864 | /* functions below are for the H1 protocol processing */ |
| 865 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 866 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 867 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 868 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 869 | 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] | 870 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 871 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 872 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 873 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 874 | (*(p + 5) > '1' || |
| 875 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 876 | h1m->flags |= H1_MF_VER_11; |
| 877 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 878 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 879 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 880 | * greater or equal to 1.1 |
| 881 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 882 | 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] | 883 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 884 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 885 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 886 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 887 | (*(p + 5) > '1' || |
| 888 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 889 | h1m->flags |= H1_MF_VER_11; |
| 890 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 891 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 892 | /* Deduce the connection mode of the client connection, depending on the |
| 893 | * configuration and the H1 message flags. This function is called twice, the |
| 894 | * first time when the request is parsed and the second time when the response |
| 895 | * is parsed. |
| 896 | */ |
| 897 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 898 | { |
| 899 | struct proxy *fe = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 900 | |
| 901 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 902 | /* Output direction: second pass */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 903 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 904 | h1s->status == 101) { |
| 905 | /* Either we've established an explicit tunnel, or we're |
| 906 | * switching the protocol. In both cases, we're very unlikely to |
| 907 | * understand the next protocols. We have to switch to tunnel |
| 908 | * mode, so that we transfer the request and responses then let |
| 909 | * this protocol pass unmodified. When we later implement |
| 910 | * specific parsers for such protocols, we'll want to check the |
| 911 | * Upgrade header which contains information about that protocol |
| 912 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 913 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 914 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 915 | 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] | 916 | } |
| 917 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 918 | /* By default the client is in KAL mode. CLOSE mode mean |
| 919 | * it is imposed by the client itself. So only change |
| 920 | * KAL mode here. */ |
| 921 | if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) { |
| 922 | /* no length known or explicit close => close */ |
| 923 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 924 | 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] | 925 | } |
| 926 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 927 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 928 | /* no explicit keep-alive and option httpclose => close */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 929 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 930 | 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] | 931 | } |
| 932 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 933 | } |
| 934 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 935 | /* Input direction: first pass */ |
| 936 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) { |
| 937 | /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 938 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 939 | 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] | 940 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */ |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 944 | if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 945 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 946 | 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); |
| 947 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* Deduce the connection mode of the client connection, depending on the |
| 951 | * configuration and the H1 message flags. This function is called twice, the |
| 952 | * first time when the request is parsed and the second time when the response |
| 953 | * is parsed. |
| 954 | */ |
| 955 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 956 | { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 957 | struct session *sess = h1s->sess; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 958 | struct proxy *be = h1s->h1c->px; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 959 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 960 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 961 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 962 | /* Input direction: second pass */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 963 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 964 | h1s->status == 101) { |
| 965 | /* Either we've established an explicit tunnel, or we're |
| 966 | * switching the protocol. In both cases, we're very unlikely to |
| 967 | * understand the next protocols. We have to switch to tunnel |
| 968 | * mode, so that we transfer the request and responses then let |
| 969 | * this protocol pass unmodified. When we later implement |
| 970 | * specific parsers for such protocols, we'll want to check the |
| 971 | * Upgrade header which contains information about that protocol |
| 972 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 973 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 974 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 975 | 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] | 976 | } |
| 977 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 978 | /* By default the server is in KAL mode. CLOSE mode mean |
| 979 | * it is imposed by haproxy itself. So only change KAL |
| 980 | * mode here. */ |
| 981 | if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO || |
| 982 | !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){ |
| 983 | /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */ |
| 984 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 985 | 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] | 986 | } |
| 987 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 988 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 989 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 990 | /* Output direction: first pass */ |
| 991 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 992 | /* explicit close => close */ |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 993 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 994 | 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] | 995 | } |
| 996 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 997 | ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 998 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 999 | (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 1000 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) { |
| 1001 | /* no explicit keep-alive option httpclose/server-close => close */ |
| 1002 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1003 | 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] | 1004 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1005 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1006 | |
| 1007 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 1008 | if (h1s->flags & H1S_F_WANT_KAL && be->disabled) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1009 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1010 | 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); |
| 1011 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1014 | 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] | 1015 | { |
| 1016 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1017 | |
| 1018 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1019 | * token is found |
| 1020 | */ |
| 1021 | 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] | 1022 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1023 | |
| 1024 | 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] | 1025 | if (!(h1m->flags & H1_MF_VER_11)) { |
| 1026 | 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] | 1027 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1028 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1029 | } |
| 1030 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1031 | if (h1m->flags & H1_MF_VER_11) { |
| 1032 | 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] | 1033 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1034 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1035 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1038 | 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] | 1039 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1040 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1041 | * token is found |
| 1042 | */ |
| 1043 | 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] | 1044 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1045 | |
| 1046 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1047 | if (!(h1m->flags & H1_MF_VER_11) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1048 | !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) { |
| 1049 | 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] | 1050 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1051 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1052 | } |
| 1053 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1054 | if (h1m->flags & H1_MF_VER_11) { |
| 1055 | 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] | 1056 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1057 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1058 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1059 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1060 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1061 | 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] | 1062 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1063 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1064 | h1_set_cli_conn_mode(h1s, h1m); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1065 | else |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1066 | h1_set_srv_conn_mode(h1s, h1m); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1069 | static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
| 1070 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1071 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1072 | h1_set_cli_conn_mode(h1s, h1m); |
| 1073 | else |
| 1074 | h1_set_srv_conn_mode(h1s, h1m); |
| 1075 | |
| 1076 | if (!(h1m->flags & H1_MF_RESP)) |
| 1077 | h1_update_req_conn_value(h1s, h1m, conn_val); |
| 1078 | else |
| 1079 | h1_update_res_conn_value(h1s, h1m, conn_val); |
| 1080 | } |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1081 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1082 | /* Try to adjust the case of the message header name using the global map |
| 1083 | * <hdrs_map>. |
| 1084 | */ |
| 1085 | static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name) |
| 1086 | { |
| 1087 | struct ebpt_node *node; |
| 1088 | struct h1_hdr_entry *entry; |
| 1089 | |
| 1090 | /* No entry in the map, do nothing */ |
| 1091 | if (eb_is_empty(&hdrs_map.map)) |
| 1092 | return; |
| 1093 | |
| 1094 | /* No conversion fo the request headers */ |
| 1095 | if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV)) |
| 1096 | return; |
| 1097 | |
| 1098 | /* No conversion fo the response headers */ |
| 1099 | if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI)) |
| 1100 | return; |
| 1101 | |
| 1102 | node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len); |
| 1103 | if (!node) |
| 1104 | return; |
| 1105 | entry = container_of(node, struct h1_hdr_entry, node); |
| 1106 | name->ptr = entry->name.ptr; |
| 1107 | name->len = entry->name.len; |
| 1108 | } |
| 1109 | |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1110 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 1111 | * The description must be small enough to always fit in a buffer. The output |
| 1112 | * buffer may be the trash so the trash must not be used inside this function. |
| 1113 | */ |
| 1114 | static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 1115 | { |
| 1116 | chunk_appendf(out, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1117 | " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n" |
| 1118 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 1119 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 1120 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 1121 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 1122 | 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] | 1123 | } |
| 1124 | /* |
| 1125 | * Capture a bad request or response and archive it in the proxy's structure. |
| 1126 | * By default it tries to report the error position as h1m->err_pos. However if |
| 1127 | * this one is not set, it will then report h1m->next, which is the last known |
| 1128 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 1129 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 1130 | * determined thanks to the h1m's flags. |
| 1131 | */ |
| 1132 | static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s, |
| 1133 | struct h1m *h1m, struct buffer *buf) |
| 1134 | { |
Christopher Faulet | db2c17d | 2020-10-15 17:19:46 +0200 | [diff] [blame] | 1135 | struct session *sess = h1s->sess; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1136 | struct proxy *proxy = h1c->px; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1137 | struct proxy *other_end; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1138 | union error_snapshot_ctx ctx; |
| 1139 | |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 1140 | if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data) { |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1141 | if (sess == NULL) |
| 1142 | sess = si_strm(h1s->cs->data)->sess; |
| 1143 | if (!(h1m->flags & H1_MF_RESP)) |
| 1144 | other_end = si_strm(h1s->cs->data)->be; |
| 1145 | else |
| 1146 | other_end = sess->fe; |
| 1147 | } else |
| 1148 | other_end = NULL; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* http-specific part now */ |
| 1151 | ctx.h1.state = h1m->state; |
| 1152 | ctx.h1.c_flags = h1c->flags; |
| 1153 | ctx.h1.s_flags = h1s->flags; |
| 1154 | ctx.h1.m_flags = h1m->flags; |
| 1155 | ctx.h1.m_clen = h1m->curr_len; |
| 1156 | ctx.h1.m_blen = h1m->body_len; |
| 1157 | |
| 1158 | proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end, |
| 1159 | h1c->conn->target, sess, buf, 0, 0, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1160 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 1161 | &ctx, h1_show_error_snapshot); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1164 | /* Emit the chunksize followed by a CRLF in front of data of the buffer |
| 1165 | * <buf>. It goes backwards and starts with the byte before the buffer's |
| 1166 | * head. The caller is responsible for ensuring there is enough room left before |
| 1167 | * the buffer's head for the string. |
| 1168 | */ |
| 1169 | static void h1_emit_chunk_size(struct buffer *buf, size_t chksz) |
| 1170 | { |
| 1171 | char *beg, *end; |
| 1172 | |
| 1173 | beg = end = b_head(buf); |
| 1174 | *--beg = '\n'; |
| 1175 | *--beg = '\r'; |
| 1176 | do { |
| 1177 | *--beg = hextab[chksz & 0xF]; |
| 1178 | } while (chksz >>= 4); |
| 1179 | buf->head -= (end - beg); |
| 1180 | b_add(buf, end - beg); |
| 1181 | } |
| 1182 | |
| 1183 | /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for |
| 1184 | * ensuring there is enough room left in the buffer for the string. */ |
| 1185 | static void h1_emit_chunk_crlf(struct buffer *buf) |
| 1186 | { |
| 1187 | *(b_peek(buf, b_data(buf))) = '\r'; |
| 1188 | *(b_peek(buf, b_data(buf) + 1)) = '\n'; |
| 1189 | b_add(buf, 2); |
| 1190 | } |
| 1191 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1192 | /* |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1193 | * Switch the request to tunnel mode. This function must only be called for |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1194 | * CONNECT requests. On the client side, if the response is not finished, the |
| 1195 | * mux is mark as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1196 | */ |
| 1197 | static void h1_set_req_tunnel_mode(struct h1s *h1s) |
| 1198 | { |
| 1199 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 1200 | h1s->req.state = H1_MSG_TUNNEL; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1201 | TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1202 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1203 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1204 | h1s->flags &= ~H1S_F_PARSING_DONE; |
| 1205 | if (h1s->res.state < H1_MSG_DONE) { |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1206 | h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE; |
| 1207 | TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1208 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1209 | } |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1210 | else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) { |
| 1211 | h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1212 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1213 | TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1214 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Switch the response to tunnel mode. This function must only be called on |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1219 | * successful replies to CONNECT requests or on protocol switching. In this |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1220 | * last case, this function takes care to switch the request to tunnel mode if |
| 1221 | * possible. On the server side, if the request is not finished, the mux is mark |
| 1222 | * as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1223 | */ |
| 1224 | static void h1_set_res_tunnel_mode(struct h1s *h1s) |
| 1225 | { |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1226 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1227 | h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 1228 | h1s->res.state = H1_MSG_TUNNEL; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1229 | TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1230 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1231 | if (h1s->h1c->flags & H1C_F_IS_BACK) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1232 | h1s->flags &= ~H1S_F_PARSING_DONE; |
| 1233 | /* On protocol switching, switch the request to tunnel mode if it is in |
| 1234 | * DONE state. Otherwise we will wait the end of the request to switch |
| 1235 | * it in tunnel mode. |
| 1236 | */ |
| 1237 | if (h1s->req.state < H1_MSG_DONE) { |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1238 | h1s->h1c->flags |= H1C_F_WAIT_OPPOSITE; |
| 1239 | TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1s->h1c->conn, h1s); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1240 | } |
| 1241 | else if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) { |
| 1242 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 1243 | h1s->req.state = H1_MSG_TUNNEL; |
| 1244 | TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1245 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1246 | } |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1247 | else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) { |
| 1248 | h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1249 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1250 | TRACE_STATE("Re-enable read on h1c", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1s->h1c->conn, h1s); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1251 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1255 | * 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] | 1256 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1257 | * flag. If relies on the function http_parse_msg_hdrs() to do the parsing. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1258 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1259 | static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1260 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1261 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1262 | union h1_sl h1sl; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1263 | int ret = 0; |
| 1264 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1265 | 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] | 1266 | |
Christopher Faulet | 89aed32 | 2020-06-02 17:33:56 +0200 | [diff] [blame] | 1267 | if (!(h1s->h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */ |
| 1268 | !(h1s->flags & H1S_F_NOT_FIRST) && /* It is the first transaction */ |
| 1269 | !(h1m->flags & H1_MF_RESP)) { /* It is a request */ |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1270 | /* Try to match H2 preface before parsing the request headers. */ |
| 1271 | ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1272 | if (ret > 0) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1273 | goto h2c_upgrade; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1274 | } |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1275 | } |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1276 | else { |
| 1277 | if (h1s->meth == HTTP_METH_CONNECT) |
| 1278 | h1m->flags |= H1_MF_METH_CONNECT; |
| 1279 | if (h1s->meth == HTTP_METH_HEAD) |
| 1280 | h1m->flags |= H1_MF_METH_HEAD; |
| 1281 | } |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1282 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1283 | ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max); |
| 1284 | if (!ret) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1285 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1286 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1287 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1288 | TRACE_USER("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] | 1289 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1290 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1291 | goto end; |
| 1292 | } |
| 1293 | |
Christopher Faulet | 486498c | 2019-10-11 14:22:00 +0200 | [diff] [blame] | 1294 | if (h1m->err_pos >= 0) { |
| 1295 | /* Maybe we found an error during the parsing while we were |
| 1296 | * configured not to block on that, so we have to capture it |
| 1297 | * now. |
| 1298 | */ |
| 1299 | TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
| 1300 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1301 | } |
| 1302 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1303 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1304 | h1s->meth = h1sl.rq.meth; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1305 | if (h1m->state == H1_MSG_TUNNEL) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1306 | h1_set_req_tunnel_mode(h1s); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1307 | } |
| 1308 | else { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1309 | h1s->status = h1sl.st.status; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1310 | if (h1m->state == H1_MSG_TUNNEL) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1311 | h1_set_res_tunnel_mode(h1s); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1312 | } |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1313 | h1_process_input_conn_mode(h1s, h1m, htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1314 | *ofs += ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1315 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1316 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1317 | 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] | 1318 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1319 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1320 | h2c_upgrade: |
| 1321 | h1s->h1c->flags |= H1C_F_UPG_H2C; |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1322 | h1s->flags |= H1S_F_PARSING_DONE; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1323 | htx->flags |= HTX_FL_UPGRADE; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1324 | TRACE_DEVEL("leaving on H2 update", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_RX_EOI, h1s->h1c->conn, h1s); |
| 1325 | return 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1329 | * 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] | 1330 | * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag. |
| 1331 | * 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] | 1332 | */ |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 1333 | static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1334 | struct buffer *buf, size_t *ofs, size_t max, |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1335 | struct buffer *htxbuf) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1336 | { |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1337 | int ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1338 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1339 | 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] | 1340 | ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1341 | if (!ret) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1342 | 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] | 1343 | if ((*htx)->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1344 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1345 | TRACE_USER("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] | 1346 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1347 | } |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1348 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1351 | *ofs += ret; |
| 1352 | |
| 1353 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1354 | 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] | 1355 | return ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1359 | * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 1360 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1361 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
| 1362 | * responsible to update the parser state <h1m>. |
| 1363 | */ |
| 1364 | static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1365 | struct buffer *buf, size_t *ofs, size_t max) |
| 1366 | { |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1367 | int ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1368 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1369 | 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] | 1370 | ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1371 | if (!ret) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1372 | 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] | 1373 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1374 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1375 | TRACE_USER("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] | 1376 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1377 | } |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1378 | goto end; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1379 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1380 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1381 | *ofs += ret; |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1382 | |
| 1383 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1384 | 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] | 1385 | return ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /* |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1389 | * Add the EOM in the HTX message. It returns 1 on success or 0 if it couldn't |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1390 | * proceed. This functions is responsible to update the parser state <h1m>. |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1391 | */ |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1392 | static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1393 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1394 | { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1395 | int ret; |
| 1396 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1397 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1398 | ret = h1_parse_msg_eom(h1m, htx, max); |
| 1399 | if (!ret) { |
| 1400 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s); |
| 1401 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1402 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1403 | TRACE_USER("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_EOI|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1404 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1405 | } |
| 1406 | goto end; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1407 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1408 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1409 | h1s->flags |= H1S_F_PARSING_DONE; |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1410 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1411 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_EOI, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1412 | return ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1416 | * 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] | 1417 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1418 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1419 | */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1420 | static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1421 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1422 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1423 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1424 | struct htx *htx; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1425 | size_t ret, data; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1426 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1427 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1428 | htx = htx_from_buf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1429 | 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] | 1430 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1431 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1432 | |
Christopher Faulet | 7402776 | 2019-02-26 14:45:05 +0100 | [diff] [blame] | 1433 | data = htx->data; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1434 | if (h1s->flags & H1S_F_PARSING_ERROR) |
Christopher Faulet | 0e54d54 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1435 | goto end; |
| 1436 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1437 | do { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1438 | size_t used = htx_used_space(htx); |
| 1439 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1440 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1441 | TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1442 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1443 | if (!ret) |
| 1444 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1445 | |
| 1446 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"), |
| 1447 | H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret}); |
| 1448 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1449 | if ((h1m->flags & H1_MF_RESP) && |
| 1450 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1451 | h1m_init_res(&h1s->res); |
| 1452 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1453 | 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] | 1454 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1455 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1456 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1457 | TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 1458 | ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1459 | if (!ret && h1m->state != H1_MSG_DONE) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1460 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1461 | |
| 1462 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"), |
| 1463 | 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] | 1464 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1465 | else if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1466 | TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s); |
| 1467 | ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
| 1468 | if (!ret && h1m->state != H1_MSG_DONE) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1469 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1470 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1471 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"), |
| 1472 | 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] | 1473 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1474 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1475 | if (!(h1s->flags & H1S_F_PARSING_DONE)) { |
| 1476 | if (!h1_process_eom(h1s, h1m, htx, &h1c->ibuf, &total, count)) |
| 1477 | break; |
| 1478 | |
| 1479 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"), |
| 1480 | H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx); |
| 1481 | } |
| 1482 | |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1483 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) |
| 1484 | h1_set_req_tunnel_mode(h1s); |
| 1485 | else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) { |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1486 | h1c->flags |= H1C_F_WAIT_OPPOSITE; |
| 1487 | TRACE_STATE("Disable read on h1c (wait_opposite)", H1_EV_RX_DATA|H1_EV_H1C_BLK, h1c->conn, h1s); |
Christopher Faulet | 23021ad | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1488 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1489 | } |
Christopher Faulet | 23021ad | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1490 | else |
| 1491 | break; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1492 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1493 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1494 | TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | af54263 | 2019-10-01 21:52:49 +0200 | [diff] [blame] | 1495 | ret = h1_process_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1496 | if (!ret) |
| 1497 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1498 | |
| 1499 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"), |
| 1500 | 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] | 1501 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1502 | else { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1503 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1504 | break; |
| 1505 | } |
| 1506 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1507 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1508 | } while (!(h1s->flags & H1S_F_PARSING_ERROR)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1509 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1510 | if (h1s->flags & H1S_F_PARSING_ERROR) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1511 | TRACE_PROTO("parsing error", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1512 | goto parsing_err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1513 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1514 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1515 | b_del(&h1c->ibuf, total); |
| 1516 | |
| 1517 | end: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1518 | htx_to_buf(htx, buf); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1519 | ret = htx->data - data; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1520 | 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] | 1521 | h1c->flags &= ~H1C_F_IN_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1522 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE); |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 1523 | 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] | 1524 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1525 | |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1526 | if (!(h1m->flags & H1_MF_CHNK) && |
| 1527 | ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) { |
| 1528 | TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
| 1529 | h1s->cs->flags |= CS_FL_MAY_SPLICE; |
| 1530 | } |
| 1531 | else { |
| 1532 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
| 1533 | h1s->cs->flags &= ~CS_FL_MAY_SPLICE; |
| 1534 | } |
| 1535 | |
| 1536 | if (h1s->flags & H1S_F_PARSING_DONE) |
| 1537 | h1s->cs->flags |= CS_FL_EOI; |
| 1538 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1539 | h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 1540 | |
Christopher Faulet | cdc90e9 | 2019-03-28 13:28:46 +0100 | [diff] [blame] | 1541 | if (!b_data(&h1c->ibuf)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1542 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 6716cc2 | 2019-12-20 17:33:24 +0100 | [diff] [blame] | 1543 | if (h1s_data_pending(h1s) && !htx_is_empty(htx)) |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1544 | h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM; |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1545 | else if (h1s->flags & H1S_F_REOS) { |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1546 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 466080d | 2019-11-15 09:50:22 +0100 | [diff] [blame] | 1547 | if (h1m->state == H1_MSG_TUNNEL) |
| 1548 | h1s->cs->flags |= CS_FL_EOI; |
| 1549 | else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) |
Christopher Faulet | b8d2ee0 | 2019-02-25 15:29:51 +0100 | [diff] [blame] | 1550 | h1s->cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1551 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1552 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1553 | 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] | 1554 | return ret; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1555 | |
| 1556 | parsing_err: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1557 | b_reset(&h1c->ibuf); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1558 | htx_to_buf(htx, buf); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1559 | h1s->cs->flags |= CS_FL_EOI; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1560 | 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] | 1561 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1564 | /* |
| 1565 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1566 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1567 | * 0 if it couldn't proceed. |
| 1568 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1569 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1570 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1571 | struct h1s *h1s = h1c->h1s; |
| 1572 | struct h1m *h1m; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1573 | struct htx *chn_htx = NULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1574 | struct htx_blk *blk; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1575 | struct buffer tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1576 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1577 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1578 | if (!count) |
| 1579 | goto end; |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1580 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1581 | chn_htx = htxbuf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1582 | TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
| 1583 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1584 | if (htx_is_empty(chn_htx)) |
| 1585 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1586 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1587 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1588 | h1c->flags |= H1C_F_OUT_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1589 | 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] | 1590 | goto end; |
| 1591 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1592 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1593 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1594 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1595 | if (h1s->flags & H1S_F_PROCESSING_ERROR) |
Christopher Faulet | 0e54d54 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1596 | goto end; |
| 1597 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1598 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1599 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1600 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1601 | /* Perform some optimizations to reduce the number of buffer copies. |
| 1602 | * First, if the mux's buffer is empty and the htx area contains |
| 1603 | * exactly one data block of the same size as the requested count, |
| 1604 | * then it's possible to simply swap the caller's buffer with the |
| 1605 | * mux's output buffer and adjust offsets and length to match the |
| 1606 | * entire DATA HTX block in the middle. In this case we perform a |
| 1607 | * true zero-copy operation from end-to-end. This is the situation |
| 1608 | * that happens all the time with large files. Second, if this is not |
| 1609 | * possible, but the mux's output buffer is empty, we still have an |
| 1610 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 1611 | * the intermediary buffer's area point to the output buffer's area. |
| 1612 | * In this case we want to skip the HTX header to make sure that copies |
| 1613 | * remain aligned and that this operation remains possible all the |
| 1614 | * time. This goes for headers, data blocks and any data extracted from |
| 1615 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1616 | */ |
| 1617 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | 192c6a2 | 2019-06-11 16:32:24 +0200 | [diff] [blame] | 1618 | if (htx_nbblks(chn_htx) == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1619 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1620 | htx_get_blk_value(chn_htx, blk).len == count) { |
| 1621 | void *old_area = h1c->obuf.area; |
| 1622 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1623 | TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1624 | h1c->obuf.area = buf->area; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1625 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1626 | h1c->obuf.data = count; |
| 1627 | |
| 1628 | buf->area = old_area; |
| 1629 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1630 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1631 | chn_htx = (struct htx *)buf->area; |
| 1632 | htx_reset(chn_htx); |
| 1633 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1634 | /* The message is chunked. We need to emit the chunk |
| 1635 | * size. We have at least the size of the struct htx to |
| 1636 | * write the chunk envelope. It should be enough. |
| 1637 | */ |
| 1638 | if (h1m->flags & H1_MF_CHNK) { |
| 1639 | h1_emit_chunk_size(&h1c->obuf, count); |
| 1640 | h1_emit_chunk_crlf(&h1c->obuf); |
| 1641 | } |
| 1642 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1643 | total += count; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1644 | if (h1m->state == H1_MSG_DATA) |
| 1645 | 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] | 1646 | 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] | 1647 | else |
| 1648 | 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] | 1649 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count}); |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1650 | goto out; |
| 1651 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1652 | tmp.area = h1c->obuf.area + h1c->obuf.head; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1653 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1654 | else |
| 1655 | tmp.area = trash.area; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1656 | |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1657 | tmp.data = 0; |
| 1658 | tmp.size = b_room(&h1c->obuf); |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1659 | while (count && !(h1s->flags & H1S_F_PROCESSING_ERROR) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1660 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1661 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1662 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1663 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 1664 | uint32_t vlen, chklen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1665 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1666 | vlen = sz; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 1667 | if (type != HTX_BLK_DATA && vlen > count) |
| 1668 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1669 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1670 | if (type == HTX_BLK_UNUSED) |
| 1671 | goto nextblk; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1672 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1673 | switch (h1m->state) { |
| 1674 | case H1_MSG_RQBEFORE: |
| 1675 | if (type != HTX_BLK_REQ_SL) |
| 1676 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1677 | 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] | 1678 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1679 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1680 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1681 | if (!h1_format_htx_reqline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1682 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1683 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 1684 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1685 | h1m->flags |= H1_MF_CLEN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1686 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1687 | if (h1c->flags & H1C_F_WAIT_OPPOSITE) { |
| 1688 | h1c->flags &= ~H1C_F_WAIT_OPPOSITE; |
| 1689 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 1690 | TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
| 1691 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1692 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1693 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1694 | case H1_MSG_RPBEFORE: |
| 1695 | if (type != HTX_BLK_RES_SL) |
| 1696 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1697 | 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] | 1698 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1699 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1700 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1701 | if (!h1_format_htx_stline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1702 | goto full; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1703 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1704 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1705 | if (sl->info.res.status < 200 && |
| 1706 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1707 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1708 | h1m->state = H1_MSG_HDR_FIRST; |
| 1709 | break; |
| 1710 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1711 | case H1_MSG_HDR_FIRST: |
| 1712 | case H1_MSG_HDR_NAME: |
| 1713 | case H1_MSG_HDR_L2_LWS: |
| 1714 | if (type == HTX_BLK_EOH) |
| 1715 | goto last_lf; |
| 1716 | if (type != HTX_BLK_HDR) |
| 1717 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1718 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1719 | h1m->state = H1_MSG_HDR_NAME; |
| 1720 | n = htx_get_blk_name(chn_htx, blk); |
| 1721 | v = htx_get_blk_value(chn_htx, blk); |
| 1722 | |
Christopher Faulet | 86d144c | 2019-08-14 16:32:25 +0200 | [diff] [blame] | 1723 | /* Skip all pseudo-headers */ |
| 1724 | if (*(n.ptr) == ':') |
| 1725 | goto skip_hdr; |
| 1726 | |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 1727 | if (isteq(n, ist("transfer-encoding"))) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1728 | h1_parse_xfer_enc_header(h1m, v); |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 1729 | else if (isteq(n, ist("content-length"))) { |
Christopher Faulet | 5220ef2 | 2019-03-27 15:44:56 +0100 | [diff] [blame] | 1730 | /* Only skip C-L header with invalid value. */ |
| 1731 | if (h1_parse_cont_len_header(h1m, &v) < 0) |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1732 | goto skip_hdr; |
| 1733 | } |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 1734 | else if (isteq(n, ist("connection"))) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1735 | h1_parse_connection_header(h1m, &v); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1736 | if (!v.len) |
| 1737 | goto skip_hdr; |
| 1738 | } |
| 1739 | |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 1740 | /* Skip header if same name is used to add the server name */ |
| 1741 | if (!(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name && |
| 1742 | isteqi(n, ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len))) |
| 1743 | goto skip_hdr; |
| 1744 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1745 | /* Try to adjust the case of the header name */ |
| 1746 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1747 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1748 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1749 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1750 | skip_hdr: |
| 1751 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1752 | break; |
| 1753 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1754 | case H1_MSG_LAST_LF: |
| 1755 | if (type != HTX_BLK_EOH) |
| 1756 | goto error; |
| 1757 | last_lf: |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1758 | h1m->state = H1_MSG_LAST_LF; |
| 1759 | if (!(h1s->flags & H1S_F_HAVE_O_CONN)) { |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 1760 | /* If the reply comes from haproxy while the request is |
| 1761 | * not finished, we force the connection close. */ |
| 1762 | if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) { |
| 1763 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 1764 | TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1765 | } |
| 1766 | |
| 1767 | /* the conn_mode must be processed. So do it */ |
Christopher Faulet | a110ecb | 2019-06-17 14:07:46 +0200 | [diff] [blame] | 1768 | n = ist("connection"); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1769 | v = ist(""); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1770 | h1_process_output_conn_mode(h1s, h1m, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1771 | if (v.len) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1772 | /* Try to adjust the case of the header name */ |
| 1773 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1774 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1775 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1776 | goto full; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1777 | } |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1778 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1779 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1780 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1781 | if ((h1s->meth != HTTP_METH_CONNECT && |
| 1782 | (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] | 1783 | (H1_MF_VER_11|H1_MF_XFER_LEN)) || |
| 1784 | (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && |
| 1785 | h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) && |
| 1786 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
| 1787 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1788 | /* chunking needed but header not seen */ |
Christopher Faulet | 7a991a9 | 2019-10-04 10:23:51 +0200 | [diff] [blame] | 1789 | n = ist("transfer-encoding"); |
| 1790 | v = ist("chunked"); |
| 1791 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1792 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1793 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1794 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1795 | 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] | 1796 | h1m->flags |= H1_MF_CHNK; |
| 1797 | } |
| 1798 | |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 1799 | /* Now add the server name to a header (if requested) */ |
| 1800 | if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) && |
| 1801 | !(h1m->flags & H1_MF_RESP) && h1c->px->server_id_hdr_name) { |
| 1802 | struct server *srv = objt_server(h1c->conn->target); |
| 1803 | |
| 1804 | if (srv) { |
| 1805 | n = ist2(h1c->px->server_id_hdr_name, h1c->px->server_id_hdr_len); |
| 1806 | v = ist(srv->id); |
Christopher Faulet | 5cef2a6 | 2019-10-02 11:06:13 +0200 | [diff] [blame] | 1807 | |
| 1808 | /* Try to adjust the case of the header name */ |
| 1809 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1810 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1811 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1812 | goto full; |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 1813 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1814 | 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] | 1815 | h1s->flags |= H1S_F_HAVE_SRV_NAME; |
| 1816 | } |
| 1817 | |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1818 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1819 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1820 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1821 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"), |
| 1822 | H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
| 1823 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1824 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
| 1825 | /* a CONNECT request is sent to the server. Switch it to tunnel mode. */ |
| 1826 | h1_set_req_tunnel_mode(h1s); |
| 1827 | } |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1828 | else if ((h1m->flags & H1_MF_RESP) && |
| 1829 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1830 | /* a successful reply to a CONNECT or a protocol switching is sent |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1831 | * to the client. Switch the response to tunnel mode. |
| 1832 | */ |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1833 | h1_set_res_tunnel_mode(h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1834 | TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1835 | } |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1836 | else if ((h1m->flags & H1_MF_RESP) && |
| 1837 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1838 | h1m_init_res(&h1s->res); |
| 1839 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1840 | h1s->flags &= ~H1S_F_HAVE_O_CONN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1841 | 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] | 1842 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1843 | else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) { |
Christopher Faulet | b8fc304 | 2019-07-01 16:17:30 +0200 | [diff] [blame] | 1844 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1845 | TRACE_STATE("HEAD response processed", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
| 1846 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1847 | else |
| 1848 | h1m->state = H1_MSG_DATA; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1849 | break; |
| 1850 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1851 | case H1_MSG_DATA: |
Christopher Faulet | f8db73e | 2019-07-04 17:12:12 +0200 | [diff] [blame] | 1852 | case H1_MSG_TUNNEL: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1853 | if (type == HTX_BLK_EOM) { |
| 1854 | /* Chunked message without explicit trailers */ |
| 1855 | if (h1m->flags & H1_MF_CHNK) { |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1856 | if (!chunk_memcat(&tmp, "0\r\n\r\n", 5)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1857 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1858 | } |
| 1859 | goto done; |
| 1860 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1861 | else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) { |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1862 | /* If the message is not chunked, never |
| 1863 | * add the last chunk. */ |
| 1864 | 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] | 1865 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1866 | 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] | 1867 | goto trailers; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1868 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1869 | else if (type != HTX_BLK_DATA) |
| 1870 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1871 | |
| 1872 | 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] | 1873 | |
| 1874 | |
| 1875 | if (vlen > count) { |
| 1876 | /* Get the maximum amount of data we can xferred */ |
| 1877 | vlen = count; |
| 1878 | } |
| 1879 | |
| 1880 | chklen = 0; |
| 1881 | if (h1m->flags & H1_MF_CHNK) { |
| 1882 | chklen = b_room(&tmp); |
| 1883 | chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 1884 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 1885 | (chklen < 1048576) ? 5 : 8); |
| 1886 | chklen += 4; /* 2 x CRLF */ |
| 1887 | } |
| 1888 | |
| 1889 | if (vlen + chklen > b_room(&tmp)) { |
| 1890 | /* too large for the buffer */ |
| 1891 | if (chklen >= b_room(&tmp)) |
| 1892 | goto full; |
| 1893 | vlen = b_room(&tmp) - chklen; |
| 1894 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1895 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1896 | v.len = vlen; |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1897 | if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1898 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1899 | |
| 1900 | if (h1m->state == H1_MSG_DATA) |
| 1901 | 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] | 1902 | 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] | 1903 | else |
| 1904 | 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] | 1905 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len}); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1906 | break; |
| 1907 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1908 | case H1_MSG_TRAILERS: |
| 1909 | if (type == HTX_BLK_EOM) |
| 1910 | goto done; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1911 | else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT) |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1912 | goto error; |
| 1913 | trailers: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1914 | h1m->state = H1_MSG_TRAILERS; |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1915 | /* If the message is not chunked, ignore |
| 1916 | * trailers. It may happen with H2 messages. */ |
| 1917 | if (!(h1m->flags & H1_MF_CHNK)) |
| 1918 | break; |
| 1919 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1920 | if (type == HTX_BLK_EOT) { |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1921 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1922 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1923 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"), |
| 1924 | H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s); |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1925 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1926 | else { // HTX_BLK_TLR |
| 1927 | n = htx_get_blk_name(chn_htx, blk); |
| 1928 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1929 | |
| 1930 | /* Try to adjust the case of the header name */ |
| 1931 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1932 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 1933 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1934 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1935 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1936 | break; |
| 1937 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1938 | case H1_MSG_DONE: |
| 1939 | if (type != HTX_BLK_EOM) |
| 1940 | goto error; |
| 1941 | done: |
| 1942 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1943 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) { |
| 1944 | h1_set_req_tunnel_mode(h1s); |
| 1945 | TRACE_STATE("switch H1 request in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
| 1946 | } |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1947 | else if (h1s->h1c->flags & H1C_F_WAIT_OPPOSITE) { |
| 1948 | h1s->h1c->flags &= ~H1C_F_WAIT_OPPOSITE; |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 1949 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1950 | TRACE_STATE("Re-enable read on h1c", H1_EV_TX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
Christopher Faulet | cd67bff | 2019-06-14 16:54:15 +0200 | [diff] [blame] | 1951 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1952 | |
| 1953 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"), |
| 1954 | H1_EV_TX_DATA, h1c->conn, h1s); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1955 | break; |
| 1956 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1957 | default: |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1958 | error: |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1959 | TRACE_PROTO("formatting error", H1_EV_TX_DATA, h1c->conn, h1s); |
Christopher Faulet | d87d3fa | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1960 | /* Unexpected error during output processing */ |
Christopher Faulet | 69b4821 | 2019-09-09 10:11:30 +0200 | [diff] [blame] | 1961 | chn_htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1962 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
Christopher Faulet | d87d3fa | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1963 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1964 | TRACE_STATE("processing error, set error on h1c/h1s", H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 1965 | TRACE_DEVEL("unexpected error", H1_EV_TX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1966 | break; |
| 1967 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1968 | |
| 1969 | nextblk: |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1970 | total += vlen; |
| 1971 | count -= vlen; |
| 1972 | if (sz == vlen) |
| 1973 | blk = htx_remove_blk(chn_htx, blk); |
| 1974 | else { |
| 1975 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 1976 | break; |
| 1977 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1980 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1981 | /* when the output buffer is empty, tmp shares the same area so that we |
| 1982 | * only have to update pointers and lengths. |
| 1983 | */ |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1984 | if (tmp.area == h1c->obuf.area + h1c->obuf.head) |
| 1985 | h1c->obuf.data = tmp.data; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1986 | else |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1987 | b_putblk(&h1c->obuf, tmp.area, tmp.data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1988 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1989 | htx_to_buf(chn_htx, buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1990 | out: |
| 1991 | if (!buf_room_for_htx_data(&h1c->obuf)) { |
| 1992 | 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] | 1993 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1994 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1995 | end: |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1996 | 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] | 1997 | return total; |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1998 | |
| 1999 | full: |
| 2000 | TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
| 2001 | h1c->flags |= H1C_F_OUT_FULL; |
| 2002 | goto copy; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2005 | /*********************************************************/ |
| 2006 | /* functions below are I/O callbacks from the connection */ |
| 2007 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2008 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 2009 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2010 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2011 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2012 | tasklet_wakeup(h1s->subs->tasklet); |
| 2013 | h1s->subs->events &= ~SUB_RETRY_RECV; |
| 2014 | if (!h1s->subs->events) |
| 2015 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 2019 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2020 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2021 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2022 | tasklet_wakeup(h1s->subs->tasklet); |
| 2023 | h1s->subs->events &= ~SUB_RETRY_SEND; |
| 2024 | if (!h1s->subs->events) |
| 2025 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2026 | } |
| 2027 | } |
| 2028 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2029 | /* |
| 2030 | * Attempt to read data, and subscribe if none available |
| 2031 | */ |
| 2032 | static int h1_recv(struct h1c *h1c) |
| 2033 | { |
| 2034 | struct connection *conn = h1c->conn; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2035 | struct h1s *h1s = h1c->h1s; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2036 | size_t ret = 0, max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2037 | int rcvd = 0; |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2038 | int flags = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2039 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2040 | TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn); |
| 2041 | |
| 2042 | if (h1c->wait_event.events & SUB_RETRY_RECV) { |
| 2043 | TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 2044 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2045 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2046 | |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2047 | if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) { |
| 2048 | TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2049 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2050 | goto end; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2051 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2052 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 2053 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 2054 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2055 | TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2056 | goto end; |
| 2057 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2058 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 2059 | /* |
| 2060 | * If we only have a small amount of data, realign it, |
| 2061 | * it's probably cheaper than doing 2 recv() calls. |
| 2062 | */ |
| 2063 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
| 2064 | b_slow_realign(&h1c->ibuf, trash.area, 0); |
| 2065 | |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2066 | /* avoid useless reads after first responses */ |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2067 | if (h1s && ((!(h1c->flags & H1C_F_IS_BACK) && h1s->req.state == H1_MSG_RQBEFORE) || |
| 2068 | ((h1c->flags & H1C_F_IS_BACK) && h1s->res.state == H1_MSG_RPBEFORE))) |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2069 | flags |= CO_RFL_READ_ONCE; |
| 2070 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 2071 | max = buf_room_for_htx_data(&h1c->ibuf); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2072 | if (max) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2073 | if (h1c->flags & H1C_F_IN_FULL) { |
| 2074 | h1c->flags &= ~H1C_F_IN_FULL; |
| 2075 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2076 | } |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2077 | |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 2078 | b_realign_if_empty(&h1c->ibuf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2079 | if (!b_data(&h1c->ibuf)) { |
| 2080 | /* try to pre-align the buffer like the rxbufs will be |
| 2081 | * to optimize memory copies. |
| 2082 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2083 | h1c->ibuf.head = sizeof(struct htx); |
| 2084 | } |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2085 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2086 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2087 | if (ret > 0) { |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2088 | TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret}); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2089 | rcvd = 1; |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 2090 | if (h1c->flags & H1C_F_CS_ATTACHED) |
Christopher Faulet | 37e3607 | 2018-12-04 15:54:12 +0100 | [diff] [blame] | 2091 | h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2092 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2093 | |
Olivier Houchard | cc3fec8 | 2019-07-26 15:11:11 +0200 | [diff] [blame] | 2094 | if (ret > 0 || !h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2095 | rcvd = 1; |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2096 | goto end; |
| 2097 | } |
| 2098 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2099 | TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2100 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2101 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2102 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2103 | if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn)) |
| 2104 | h1_wake_stream_for_recv(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2105 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2106 | if (conn_xprt_read0_pending(conn) && h1s) { |
| 2107 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2108 | TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2109 | rcvd = 1; |
| 2110 | } |
| 2111 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2112 | if (!b_data(&h1c->ibuf)) |
| 2113 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2114 | else if (!buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2115 | h1c->flags |= H1C_F_IN_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2116 | TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2117 | } |
| 2118 | |
| 2119 | TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2120 | return rcvd; |
| 2121 | } |
| 2122 | |
| 2123 | |
| 2124 | /* |
| 2125 | * Try to send data if possible |
| 2126 | */ |
| 2127 | static int h1_send(struct h1c *h1c) |
| 2128 | { |
| 2129 | struct connection *conn = h1c->conn; |
| 2130 | unsigned int flags = 0; |
| 2131 | size_t ret; |
| 2132 | int sent = 0; |
| 2133 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2134 | TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn); |
| 2135 | |
| 2136 | if (conn->flags & CO_FL_ERROR) { |
| 2137 | TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2138 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2139 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2140 | |
| 2141 | if (!b_data(&h1c->obuf)) |
| 2142 | goto end; |
| 2143 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2144 | if (h1c->flags & H1C_F_CO_MSG_MORE) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2145 | flags |= CO_SFL_MSG_MORE; |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2146 | if (h1c->flags & H1C_F_CO_STREAMER) |
| 2147 | flags |= CO_SFL_STREAMER; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2148 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2149 | 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] | 2150 | if (ret > 0) { |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2151 | 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] | 2152 | if (h1c->flags & H1C_F_OUT_FULL) { |
| 2153 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 2154 | TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn); |
| 2155 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2156 | b_del(&h1c->obuf, ret); |
| 2157 | sent = 1; |
| 2158 | } |
| 2159 | |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2160 | if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2161 | TRACE_DEVEL("connection error or output closed", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2162 | /* error or output closed, nothing to send, clear the buffer to release it */ |
| 2163 | b_reset(&h1c->obuf); |
| 2164 | } |
| 2165 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2166 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2167 | if (!(h1c->flags & H1C_F_OUT_FULL)) |
| 2168 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2169 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2170 | /* We're done, no more to send */ |
| 2171 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2172 | TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2173 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2174 | if (h1c->flags & H1C_F_CS_SHUTDOWN) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2175 | TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2176 | h1_shutw_conn(conn, CS_SHW_NORMAL); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2177 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2178 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2179 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) { |
| 2180 | 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] | 2181 | 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] | 2182 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2183 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2184 | TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2185 | return sent; |
| 2186 | } |
| 2187 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2188 | |
| 2189 | /* callback called on any event by the connection handler. |
| 2190 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2191 | * destruction of the connection. |
| 2192 | */ |
| 2193 | static int h1_process(struct h1c * h1c) |
| 2194 | { |
| 2195 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2196 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2197 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2198 | TRACE_ENTER(H1_EV_H1C_WAKE, conn); |
| 2199 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2200 | if (!conn->ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2201 | return -1; |
| 2202 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2203 | if (!h1s) { |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2204 | if ((h1c->flags & H1C_F_CS_ERROR) || |
| 2205 | ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) || |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2206 | conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2207 | goto release; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2208 | if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2209 | TRACE_STATE("K/A incoming connection, create new H1 stream", H1_EV_H1C_WAKE, conn); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 2210 | if (!h1c_frt_stream_new(h1c)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2211 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2212 | } |
Christopher Faulet | 1a7ad7a | 2018-12-04 16:10:44 +0100 | [diff] [blame] | 2213 | else |
Olivier Houchard | e728478 | 2018-12-06 18:54:54 +0100 | [diff] [blame] | 2214 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2215 | h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2218 | if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) { |
| 2219 | TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn); |
| 2220 | h1_wake_stream_for_recv(h1s); |
| 2221 | } |
| 2222 | |
Christopher Faulet | 4e74155 | 2020-09-30 13:47:09 +0200 | [diff] [blame] | 2223 | if (b_data(&h1c->ibuf) && h1s->sess->t_idle == -1) |
| 2224 | h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2225 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2226 | if (conn_xprt_read0_pending(conn)) { |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2227 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2228 | TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s); |
| 2229 | } |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2230 | |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 2231 | if (!h1s_data_pending(h1s) && h1s && (h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake && |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2232 | (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR || |
Olivier Houchard | 32d75ed | 2019-01-14 17:27:23 +0100 | [diff] [blame] | 2233 | conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2234 | if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2235 | h1s->cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2236 | TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2237 | h1s->cs->data_cb->wake(h1s->cs); |
| 2238 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2239 | end: |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 2240 | h1_refresh_timeout(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2241 | TRACE_LEAVE(H1_EV_H1C_WAKE, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2242 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2243 | |
| 2244 | release: |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2245 | h1_release(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2246 | TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2247 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2251 | { |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2252 | struct connection *conn; |
| 2253 | struct tasklet *tl = (struct tasklet *)t; |
| 2254 | int conn_in_list; |
| 2255 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2256 | int ret = 0; |
| 2257 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2258 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2259 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2260 | if (tl->context == NULL) { |
| 2261 | /* The connection has been taken over by another thread, |
| 2262 | * we're no longer responsible for it, so just free the |
| 2263 | * tasklet, and do nothing. |
| 2264 | */ |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2265 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2266 | tasklet_free(tl); |
| 2267 | return NULL; |
| 2268 | } |
| 2269 | h1c = ctx; |
| 2270 | conn = h1c->conn; |
| 2271 | |
| 2272 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
| 2273 | |
| 2274 | /* Remove the connection from the list, to be sure nobody attempts |
| 2275 | * to use it while we handle the I/O events |
| 2276 | */ |
| 2277 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 2278 | if (conn_in_list) |
| 2279 | MT_LIST_DEL(&conn->list); |
| 2280 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2281 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2282 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2283 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2284 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2285 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2286 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2287 | if (ret || !h1c->h1s) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2288 | ret = h1_process(h1c); |
| 2289 | /* If we were in an idle list, we want to add it back into it, |
| 2290 | * unless h1_process() returned -1, which mean it has destroyed |
| 2291 | * the connection (testing !ret is enough, if h1_process() wasn't |
| 2292 | * called then ret will be 0 anyway. |
| 2293 | */ |
| 2294 | if (!ret && conn_in_list) { |
| 2295 | struct server *srv = objt_server(conn->target); |
| 2296 | |
| 2297 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 2298 | MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2299 | else |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 2300 | MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2301 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2302 | return NULL; |
| 2303 | } |
| 2304 | |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2305 | static void h1_reset(struct connection *conn) |
| 2306 | { |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2307 | |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2308 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2309 | |
| 2310 | static int h1_wake(struct connection *conn) |
| 2311 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2312 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2313 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2314 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2315 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
| 2316 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2317 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2318 | ret = h1_process(h1c); |
| 2319 | if (ret == 0) { |
| 2320 | struct h1s *h1s = h1c->h1s; |
| 2321 | |
Christopher Faulet | bb8baf4 | 2020-09-29 15:18:40 +0200 | [diff] [blame] | 2322 | if ((h1c->flags & H1C_F_CS_ATTACHED) && h1s->cs->data_cb->wake) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2323 | TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2324 | ret = h1s->cs->data_cb->wake(h1s->cs); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2325 | } |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2326 | } |
| 2327 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2328 | } |
| 2329 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2330 | /* Connection timeout management. The principle is that if there's no receipt |
| 2331 | * nor sending for a certain amount of time, the connection is closed. |
| 2332 | */ |
| 2333 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state) |
| 2334 | { |
| 2335 | struct h1c *h1c = context; |
| 2336 | int expired = tick_is_expired(t->expire, now_ms); |
| 2337 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2338 | TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2339 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 2340 | if (h1c) { |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2341 | /* Make sure nobody stole the connection from us */ |
| 2342 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 2343 | |
| 2344 | /* Somebody already stole the connection from us, so we should not |
| 2345 | * free it, we just have to free the task. |
| 2346 | */ |
| 2347 | if (!t->context) { |
| 2348 | h1c = NULL; |
| 2349 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 2350 | goto do_leave; |
| 2351 | } |
| 2352 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 2353 | if (!expired) { |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2354 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 2355 | 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] | 2356 | return t; |
| 2357 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2358 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2359 | /* If a conn-stream is still attached to the mux, wait for the |
| 2360 | * stream's timeout |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 2361 | */ |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2362 | if (h1c->flags & H1C_F_CS_ATTACHED) { |
| 2363 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 2364 | t->expire = TICK_ETERNITY; |
| 2365 | TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s); |
| 2366 | return t; |
| 2367 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2368 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2369 | /* We're about to destroy the connection, so make sure nobody attempts |
| 2370 | * to steal it from us. |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 2371 | */ |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2372 | if (h1c->conn->flags & CO_FL_LIST_MASK) |
Olivier Houchard | 48ce6a3 | 2020-07-02 11:58:05 +0200 | [diff] [blame] | 2373 | MT_LIST_DEL(&h1c->conn->list); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2374 | |
Olivier Houchard | f8f4c2e | 2020-06-29 20:15:59 +0200 | [diff] [blame] | 2375 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 2376 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2377 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2378 | do_leave: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2379 | task_destroy(t); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2380 | |
| 2381 | if (!h1c) { |
| 2382 | /* resources were already deleted */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2383 | TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2384 | return NULL; |
| 2385 | } |
| 2386 | |
| 2387 | h1c->task = NULL; |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 2388 | h1_release(h1c); |
| 2389 | TRACE_LEAVE(H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2390 | return NULL; |
| 2391 | } |
| 2392 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2393 | /*******************************************/ |
| 2394 | /* functions below are used by the streams */ |
| 2395 | /*******************************************/ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2396 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2397 | /* |
| 2398 | * Attach a new stream to a connection |
| 2399 | * (Used for outgoing connections) |
| 2400 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2401 | static struct conn_stream *h1_attach(struct connection *conn, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2402 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2403 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2404 | struct conn_stream *cs = NULL; |
| 2405 | struct h1s *h1s; |
| 2406 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2407 | TRACE_ENTER(H1_EV_STRM_NEW, conn); |
| 2408 | if (h1c->flags & H1C_F_CS_ERROR) { |
| 2409 | TRACE_DEVEL("leaving on h1c error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2410 | goto end; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2411 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 236c93b | 2020-07-02 09:19:54 +0200 | [diff] [blame] | 2413 | cs = cs_new(h1c->conn, h1c->conn->target); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2414 | if (!cs) { |
| 2415 | TRACE_DEVEL("leaving on CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2416 | goto end; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2417 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2418 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 2419 | h1s = h1c_bck_stream_new(h1c, cs, sess); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2420 | if (h1s == NULL) { |
| 2421 | TRACE_DEVEL("leaving on h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2422 | goto end; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2423 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2424 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2425 | TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2426 | return cs; |
| 2427 | end: |
| 2428 | cs_free(cs); |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | |
| 2432 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 2433 | * this mux, it's easy as we can only store a single conn_stream. |
| 2434 | */ |
| 2435 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 2436 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2437 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2438 | struct h1s *h1s = h1c->h1s; |
| 2439 | |
| 2440 | if (h1s) |
| 2441 | return h1s->cs; |
| 2442 | |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2446 | static void h1_destroy(void *ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2447 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2448 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2449 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2450 | TRACE_POINT(H1_EV_H1C_END, h1c->conn); |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 2451 | if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2452 | h1_release(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * Detach the stream from the connection and possibly release the connection. |
| 2457 | */ |
| 2458 | static void h1_detach(struct conn_stream *cs) |
| 2459 | { |
| 2460 | struct h1s *h1s = cs->ctx; |
| 2461 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2462 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2463 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2464 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2465 | TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s); |
| 2466 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2467 | cs->ctx = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2468 | if (!h1s) { |
| 2469 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2470 | return; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2471 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2472 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2473 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2474 | h1c = h1s->h1c; |
| 2475 | h1s->cs = NULL; |
| 2476 | |
Christopher Faulet | 42849b0 | 2020-10-05 11:35:17 +0200 | [diff] [blame] | 2477 | sess->accept_date = date; |
| 2478 | sess->tv_accept = now; |
| 2479 | sess->t_handshake = 0; |
| 2480 | sess->t_idle = -1; |
| 2481 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2482 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 2483 | h1s_destroy(h1s); |
| 2484 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2485 | if ((h1c->flags & H1C_F_IS_BACK) && (h1c->flags & H1C_F_CS_IDLE)) { |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2486 | /* If there are any excess server data in the input buffer, |
| 2487 | * release it and close the connection ASAP (some data may |
| 2488 | * remain in the output buffer). This happens if a server sends |
| 2489 | * invalid responses. So in such case, we don't want to reuse |
| 2490 | * the connection |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2491 | */ |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2492 | if (b_data(&h1c->ibuf)) { |
| 2493 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2494 | h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTDOWN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2495 | 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] | 2496 | goto release; |
| 2497 | } |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2498 | |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 2499 | if (h1c->conn->flags & CO_FL_PRIVATE) { |
| 2500 | /* Add the connection in the session server list, if not already done */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2501 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 2502 | h1c->conn->owner = NULL; |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 2503 | h1c->conn->mux->destroy(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2504 | goto end; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2505 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 2506 | /* Always idle at this step */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 2507 | if (session_check_idle_conn(sess, h1c->conn)) { |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2508 | /* The connection got destroyed, let's leave */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2509 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 2510 | goto end; |
| 2511 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2512 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 2513 | else { |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 2514 | if (h1c->conn->owner == sess) |
| 2515 | h1c->conn->owner = NULL; |
Olivier Houchard | 3c49c1b | 2020-03-22 19:56:03 +0100 | [diff] [blame] | 2516 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Olivier Houchard | dc2f275 | 2020-02-13 19:12:07 +0100 | [diff] [blame] | 2517 | 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] | 2518 | /* The server doesn't want it, let's kill the connection right away */ |
| 2519 | h1c->conn->mux->destroy(h1c); |
| 2520 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 2521 | goto end; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2522 | } |
Olivier Houchard | 199d4fa | 2020-03-22 23:25:51 +0100 | [diff] [blame] | 2523 | /* At this point, the connection has been added to the |
| 2524 | * server idle list, so another thread may already have |
| 2525 | * hijacked it, so we can't do anything with it. |
| 2526 | */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 2527 | return; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2531 | release: |
Christopher Faulet | 3ac0f43 | 2019-06-28 17:41:42 +0200 | [diff] [blame] | 2532 | /* We don't want to close right now unless the connection is in error or shut down for writes */ |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2533 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_UPG_H2C)) || |
Christopher Faulet | 37243bc | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 2534 | (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) || |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2535 | ((h1c->flags & H1C_F_CS_SHUTDOWN) && !b_data(&h1c->obuf)) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2536 | !h1c->conn->owner) { |
| 2537 | TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn); |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2538 | h1_release(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2539 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2540 | else { |
Olivier Houchard | 6966441 | 2020-03-25 12:24:11 +0100 | [diff] [blame] | 2541 | /* If we have a new request, process it immediately */ |
Olivier Houchard | c3500c3 | 2020-03-25 17:05:21 +0100 | [diff] [blame] | 2542 | if (unlikely(b_data(&h1c->ibuf))) |
Olivier Houchard | 6966441 | 2020-03-25 12:24:11 +0100 | [diff] [blame] | 2543 | h1_process(h1c); |
| 2544 | else |
| 2545 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 2546 | h1_refresh_timeout(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2547 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2548 | end: |
| 2549 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | |
| 2553 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2554 | { |
| 2555 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2556 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2557 | |
| 2558 | if (!h1s) |
| 2559 | return; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2560 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2561 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2562 | TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2563 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2564 | if (cs->flags & CS_FL_SHR) |
| 2565 | goto end; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2566 | if (cs->flags & CS_FL_KILL_CONN) { |
| 2567 | TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2568 | goto do_shutr; |
| 2569 | } |
| 2570 | if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
| 2571 | TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2572 | goto do_shutr; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2573 | } |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2574 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2575 | if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) { |
| 2576 | TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2577 | goto end; |
| 2578 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2579 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2580 | do_shutr: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2581 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 2582 | if (cs->flags & CS_FL_SHR) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2583 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2584 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2585 | cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx, |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2586 | (mode == CS_SHR_DRAIN)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2587 | end: |
| 2588 | TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2592 | { |
| 2593 | struct h1s *h1s = cs->ctx; |
| 2594 | struct h1c *h1c; |
| 2595 | |
| 2596 | if (!h1s) |
| 2597 | return; |
| 2598 | h1c = h1s->h1c; |
| 2599 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2600 | TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2601 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2602 | if (cs->flags & CS_FL_SHW) |
| 2603 | goto end; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2604 | if (cs->flags & CS_FL_KILL_CONN) { |
| 2605 | 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] | 2606 | goto do_shutw; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2607 | } |
| 2608 | if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
| 2609 | TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2610 | goto do_shutw; |
| 2611 | } |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 2612 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 2613 | if ((h1c->flags & H1C_F_UPG_H2C) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2614 | ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) { |
| 2615 | TRACE_STATE("keep connection alive (upg_h2c|want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 2616 | goto end; |
| 2617 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2618 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2619 | do_shutw: |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 2620 | h1c->flags |= H1C_F_CS_SHUTDOWN; |
| 2621 | if (!b_data(&h1c->obuf)) |
| 2622 | h1_shutw_conn(cs->conn, mode); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2623 | end: |
| 2624 | TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2627 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2628 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2629 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2630 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2631 | TRACE_ENTER(H1_EV_STRM_SHUT, conn, h1c->h1s); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2632 | conn_xprt_shutw(conn); |
| 2633 | conn_sock_shutw(conn, (mode == CS_SHW_NORMAL)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2634 | TRACE_LEAVE(H1_EV_STRM_SHUT, conn, h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2637 | /* Called from the upper layer, to unsubscribe <es> from events <event_type> |
| 2638 | * The <es> pointer is not allowed to differ from the one passed to the |
| 2639 | * subscribe() call. It always returns zero. |
| 2640 | */ |
| 2641 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2642 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2643 | struct h1s *h1s = cs->ctx; |
| 2644 | |
| 2645 | if (!h1s) |
| 2646 | return 0; |
| 2647 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2648 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2649 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2650 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2651 | es->events &= ~event_type; |
| 2652 | if (!es->events) |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2653 | h1s->subs = NULL; |
| 2654 | |
| 2655 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2656 | TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2657 | |
| 2658 | if (event_type & SUB_RETRY_SEND) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2659 | TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2660 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2661 | return 0; |
| 2662 | } |
| 2663 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2664 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 2665 | * event subscriber <es> is not allowed to change from a previous call as long |
| 2666 | * as at least one event is still subscribed. The <event_type> must only be a |
| 2667 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless |
| 2668 | * the conn_stream <cs> was already detached, in which case it will return -1. |
| 2669 | */ |
| 2670 | static int h1_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2671 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2672 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 51bb185 | 2019-09-04 10:22:34 +0200 | [diff] [blame] | 2673 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2674 | |
| 2675 | if (!h1s) |
| 2676 | return -1; |
| 2677 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2678 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2679 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2680 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 2681 | es->events |= event_type; |
| 2682 | h1s->subs = es; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2683 | |
| 2684 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2685 | TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2686 | |
| 2687 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2688 | if (event_type & SUB_RETRY_SEND) { |
| 2689 | TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2690 | /* |
| 2691 | * If the conn_stream attempt to subscribe, and the |
| 2692 | * mux isn't subscribed to the connection, then it |
| 2693 | * probably means the connection wasn't established |
| 2694 | * yet, so we have to subscribe. |
| 2695 | */ |
| 2696 | h1c = h1s->h1c; |
| 2697 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
| 2698 | h1c->conn->xprt->subscribe(h1c->conn, |
| 2699 | h1c->conn->xprt_ctx, |
| 2700 | SUB_RETRY_SEND, |
| 2701 | &h1c->wait_event); |
| 2702 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2703 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | /* Called from the upper layer, to receive data */ |
| 2707 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2708 | { |
| 2709 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2710 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2711 | 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] | 2712 | size_t ret = 0; |
| 2713 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2714 | TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2715 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 2716 | ret = h1_process_input(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2717 | else |
| 2718 | 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] | 2719 | |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2720 | if (flags & CO_RFL_BUF_FLUSH) { |
Christopher Faulet | 2eaf309 | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 2721 | if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) { |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2722 | h1c->flags |= H1C_F_WANT_SPLICE; |
| 2723 | TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2724 | } |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2725 | } |
Olivier Houchard | 02bac85 | 2019-08-22 18:34:25 +0200 | [diff] [blame] | 2726 | else { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2727 | if (h1m->state != H1_MSG_DONE && !(h1c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 2728 | 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] | 2729 | } |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2730 | 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] | 2731 | return ret; |
| 2732 | } |
| 2733 | |
| 2734 | |
| 2735 | /* Called from the upper layer, to send data */ |
| 2736 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2737 | { |
| 2738 | struct h1s *h1s = cs->ctx; |
| 2739 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2740 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2741 | |
| 2742 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2743 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2744 | h1c = h1s->h1c; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2746 | 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] | 2747 | |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 2748 | /* If we're not connected yet, or we're waiting for a handshake, stop |
| 2749 | * now, as we don't want to remove everything from the channel buffer |
| 2750 | * before we're sure we can send it. |
| 2751 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 2752 | if (h1c->conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2753 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2754 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2755 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2756 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2757 | /* Inherit some flags from the upper layer */ |
| 2758 | h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
| 2759 | if (flags & CO_SFL_MSG_MORE) |
| 2760 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 2761 | if (flags & CO_SFL_STREAMER) |
| 2762 | h1c->flags |= H1C_F_CO_STREAMER; |
| 2763 | |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2764 | while (count) { |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2765 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2766 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2767 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
| 2768 | ret = h1_process_output(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2769 | else |
| 2770 | 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] | 2771 | |
| 2772 | if ((count - ret) > 0) |
| 2773 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 2774 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2775 | if (!ret) |
| 2776 | break; |
| 2777 | total += ret; |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2778 | count -= ret; |
Olivier Houchard | 68787ef | 2020-01-15 19:13:32 +0100 | [diff] [blame] | 2779 | if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c)) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2780 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2781 | } |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 2782 | h1_refresh_timeout(h1c); |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2783 | 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] | 2784 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2787 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2788 | /* Send and get, using splicing */ |
| 2789 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 2790 | { |
| 2791 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2792 | struct h1c *h1c = h1s->h1c; |
| 2793 | 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] | 2794 | int ret = 0; |
| 2795 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2796 | TRACE_ENTER(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2797 | |
Christopher Faulet | 9fa40c4 | 2019-11-05 16:24:27 +0100 | [diff] [blame] | 2798 | 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] | 2799 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2800 | TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, cs->conn, h1s); |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2801 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2802 | TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, cs->conn, h1s); |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2803 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2804 | } |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2805 | goto end; |
| 2806 | } |
| 2807 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2808 | if (!(h1c->flags & H1C_F_WANT_SPLICE)) { |
| 2809 | h1c->flags |= H1C_F_WANT_SPLICE; |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2810 | TRACE_STATE("Block xprt rcv_buf to perform splicing", H1_EV_STRM_RECV, cs->conn, h1s); |
| 2811 | } |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2812 | if (h1s_data_pending(h1s)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2813 | TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, cs->conn, h1s); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2814 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2817 | if (!h1_recv_allowed(h1c)) { |
Christopher Faulet | 0060be9 | 2020-07-03 15:02:25 +0200 | [diff] [blame] | 2818 | TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, cs->conn, h1s); |
| 2819 | goto end; |
| 2820 | } |
| 2821 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2822 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 2823 | count = h1m->curr_len; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2824 | ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count); |
Christopher Faulet | 1146f97 | 2019-05-29 14:35:24 +0200 | [diff] [blame] | 2825 | if (h1m->state == H1_MSG_DATA && ret >= 0) { |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2826 | h1m->curr_len -= ret; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2827 | if (!h1m->curr_len) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2828 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2829 | TRACE_STATE("Allow xprt rcv_buf on !curr_len", H1_EV_STRM_RECV, cs->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2830 | } |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2831 | } |
| 2832 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2833 | end: |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2834 | if (conn_xprt_read0_pending(cs->conn)) { |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2835 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2836 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2837 | TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, cs->conn, h1s); |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2838 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2839 | |
Christopher Faulet | a131a8f | 2020-07-07 10:56:40 +0200 | [diff] [blame] | 2840 | if ((h1s->flags & H1S_F_REOS) || |
| 2841 | (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) || |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 2842 | (h1m->state == H1_MSG_DATA && !h1m->curr_len)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 2843 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s); |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2844 | cs->flags &= ~CS_FL_MAY_SPLICE; |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 2845 | } |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2846 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2847 | TRACE_LEAVE(H1_EV_STRM_RECV, cs->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2848 | return ret; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 2852 | { |
| 2853 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2854 | int ret = 0; |
| 2855 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2856 | TRACE_ENTER(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){pipe->data}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2857 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2858 | if (b_data(&h1s->h1c->obuf)) |
| 2859 | goto end; |
| 2860 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2861 | ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2862 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2863 | if (pipe->data) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2864 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) { |
| 2865 | TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, cs->conn, h1s); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2866 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2867 | } |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2868 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2869 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2870 | TRACE_LEAVE(H1_EV_STRM_SEND, cs->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2871 | return ret; |
| 2872 | } |
| 2873 | #endif |
| 2874 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2875 | static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 2876 | { |
| 2877 | int ret = 0; |
| 2878 | switch (mux_ctl) { |
| 2879 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 2880 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2881 | ret |= MUX_STATUS_READY; |
| 2882 | return ret; |
| 2883 | default: |
| 2884 | return -1; |
| 2885 | } |
| 2886 | } |
| 2887 | |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2888 | /* for debugging with CLI's "show fd" command */ |
| 2889 | static void h1_show_fd(struct buffer *msg, struct connection *conn) |
| 2890 | { |
| 2891 | struct h1c *h1c = conn->ctx; |
| 2892 | struct h1s *h1s = h1c->h1s; |
| 2893 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 2894 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 2895 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2896 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 2897 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
| 2898 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 2899 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 2900 | |
| 2901 | if (h1s) { |
| 2902 | char *method; |
| 2903 | |
| 2904 | if (h1s->meth < HTTP_METH_OTHER) |
| 2905 | method = http_known_methods[h1s->meth].ptr; |
| 2906 | else |
| 2907 | method = "UNKNOWN"; |
| 2908 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s" |
| 2909 | " .meth=%s status=%d", |
| 2910 | h1s, h1s->flags, |
| 2911 | h1m_state_str(h1s->req.state), |
| 2912 | h1m_state_str(h1s->res.state), method, h1s->status); |
| 2913 | if (h1s->cs) |
| 2914 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 2915 | h1s->cs->flags, h1s->cs->data); |
| 2916 | } |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | |
| 2920 | /* Add an entry in the headers map. Returns -1 on error and 0 on success. */ |
| 2921 | static int add_hdr_case_adjust(const char *from, const char *to, char **err) |
| 2922 | { |
| 2923 | struct h1_hdr_entry *entry; |
| 2924 | |
| 2925 | /* Be sure there is a non-empty <to> */ |
| 2926 | if (!strlen(to)) { |
| 2927 | memprintf(err, "expect <to>"); |
| 2928 | return -1; |
| 2929 | } |
| 2930 | |
| 2931 | /* Be sure only the case differs between <from> and <to> */ |
| 2932 | if (strcasecmp(from, to)) { |
| 2933 | memprintf(err, "<from> and <to> must not differ execpt the case"); |
| 2934 | return -1; |
| 2935 | } |
| 2936 | |
| 2937 | /* Be sure <from> does not already existsin the tree */ |
| 2938 | if (ebis_lookup(&hdrs_map.map, from)) { |
| 2939 | memprintf(err, "duplicate entry '%s'", from); |
| 2940 | return -1; |
| 2941 | } |
| 2942 | |
| 2943 | /* Create the entry and insert it in the tree */ |
| 2944 | entry = malloc(sizeof(*entry)); |
| 2945 | if (!entry) { |
| 2946 | memprintf(err, "out of memory"); |
| 2947 | return -1; |
| 2948 | } |
| 2949 | |
| 2950 | entry->node.key = strdup(from); |
| 2951 | entry->name.ptr = strdup(to); |
| 2952 | entry->name.len = strlen(to); |
| 2953 | if (!entry->node.key || !entry->name.ptr) { |
| 2954 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2955 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2956 | free(entry); |
| 2957 | memprintf(err, "out of memory"); |
| 2958 | return -1; |
| 2959 | } |
| 2960 | ebis_insert(&hdrs_map.map, &entry->node); |
| 2961 | return 0; |
| 2962 | } |
| 2963 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2964 | /* Migrate the the connection to the current thread. |
| 2965 | * Return 0 if successful, non-zero otherwise. |
| 2966 | * Expected to be called with the old thread lock held. |
| 2967 | */ |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 2968 | static int h1_takeover(struct connection *conn, int orig_tid) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2969 | { |
| 2970 | struct h1c *h1c = conn->ctx; |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 2971 | struct task *task; |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2972 | |
| 2973 | if (fd_takeover(conn->handle.fd, conn) != 0) |
| 2974 | return -1; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 2975 | |
| 2976 | if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) { |
| 2977 | /* We failed to takeover the xprt, even if the connection may |
| 2978 | * still be valid, flag it as error'd, as we have already |
| 2979 | * taken over the fd, and wake the tasklet, so that it will |
| 2980 | * destroy it. |
| 2981 | */ |
| 2982 | conn->flags |= CO_FL_ERROR; |
| 2983 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
| 2984 | return -1; |
| 2985 | } |
| 2986 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 2987 | if (h1c->wait_event.events) |
| 2988 | h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 2989 | h1c->wait_event.events, &h1c->wait_event); |
| 2990 | /* To let the tasklet know it should free itself, and do nothing else, |
| 2991 | * set its context to NULL. |
| 2992 | */ |
| 2993 | h1c->wait_event.tasklet->context = NULL; |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 2994 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 2995 | |
| 2996 | task = h1c->task; |
| 2997 | if (task) { |
| 2998 | task->context = NULL; |
| 2999 | h1c->task = NULL; |
| 3000 | __ha_barrier_store(); |
| 3001 | task_kill(task); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3002 | |
| 3003 | h1c->task = task_new(tid_bit); |
| 3004 | if (!h1c->task) { |
| 3005 | h1_release(h1c); |
| 3006 | return -1; |
| 3007 | } |
| 3008 | h1c->task->process = h1_timeout_task; |
| 3009 | h1c->task->context = h1c; |
| 3010 | } |
| 3011 | h1c->wait_event.tasklet = tasklet_new(); |
| 3012 | if (!h1c->wait_event.tasklet) { |
| 3013 | h1_release(h1c); |
| 3014 | return -1; |
| 3015 | } |
| 3016 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 3017 | h1c->wait_event.tasklet->context = h1c; |
| 3018 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 3019 | SUB_RETRY_RECV, &h1c->wait_event); |
| 3020 | |
| 3021 | return 0; |
| 3022 | } |
| 3023 | |
| 3024 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3025 | static void h1_hdeaders_case_adjust_deinit() |
| 3026 | { |
| 3027 | struct ebpt_node *node, *next; |
| 3028 | struct h1_hdr_entry *entry; |
| 3029 | |
| 3030 | node = ebpt_first(&hdrs_map.map); |
| 3031 | while (node) { |
| 3032 | next = ebpt_next(node); |
| 3033 | ebpt_delete(node); |
| 3034 | entry = container_of(node, struct h1_hdr_entry, node); |
| 3035 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3036 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3037 | free(entry); |
| 3038 | node = next; |
| 3039 | } |
| 3040 | free(hdrs_map.name); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3041 | } |
| 3042 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3043 | static int cfg_h1_headers_case_adjust_postparser() |
| 3044 | { |
| 3045 | FILE *file = NULL; |
| 3046 | char *c, *key_beg, *key_end, *value_beg, *value_end; |
| 3047 | char *err; |
| 3048 | int rc, line = 0, err_code = 0; |
| 3049 | |
| 3050 | if (!hdrs_map.name) |
| 3051 | goto end; |
| 3052 | |
| 3053 | file = fopen(hdrs_map.name, "r"); |
| 3054 | if (!file) { |
| 3055 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n", |
| 3056 | hdrs_map.name); |
| 3057 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3058 | goto end; |
| 3059 | } |
| 3060 | |
| 3061 | /* now parse all lines. The file may contain only two header name per |
| 3062 | * line, separated by spaces. All heading and trailing spaces will be |
| 3063 | * ignored. Lines starting with a # are ignored. |
| 3064 | */ |
| 3065 | while (fgets(trash.area, trash.size, file) != NULL) { |
| 3066 | line++; |
| 3067 | c = trash.area; |
| 3068 | |
| 3069 | /* strip leading spaces and tabs */ |
| 3070 | while (*c == ' ' || *c == '\t') |
| 3071 | c++; |
| 3072 | |
| 3073 | /* ignore emptu lines, or lines beginning with a dash */ |
| 3074 | if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n') |
| 3075 | continue; |
| 3076 | |
| 3077 | /* look for the end of the key */ |
| 3078 | key_beg = c; |
| 3079 | while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 3080 | c++; |
| 3081 | key_end = c; |
| 3082 | |
| 3083 | /* strip middle spaces and tabs */ |
| 3084 | while (*c == ' ' || *c == '\t') |
| 3085 | c++; |
| 3086 | |
| 3087 | /* look for the end of the value, it is the end of the line */ |
| 3088 | value_beg = c; |
| 3089 | while (*c && *c != '\n' && *c != '\r') |
| 3090 | c++; |
| 3091 | value_end = c; |
| 3092 | |
| 3093 | /* trim possibly trailing spaces and tabs */ |
| 3094 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 3095 | value_end--; |
| 3096 | |
| 3097 | /* set final \0 and check entries */ |
| 3098 | *key_end = '\0'; |
| 3099 | *value_end = '\0'; |
| 3100 | |
| 3101 | err = NULL; |
| 3102 | rc = add_hdr_case_adjust(key_beg, value_beg, &err); |
| 3103 | if (rc < 0) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3104 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 3105 | hdrs_map.name, err, line); |
| 3106 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 3107 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3108 | goto end; |
| 3109 | } |
| 3110 | if (rc > 0) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3111 | ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 3112 | hdrs_map.name, err, line); |
| 3113 | err_code |= ERR_WARN; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 3114 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | end: |
| 3119 | if (file) |
| 3120 | fclose(file); |
| 3121 | hap_register_post_deinit(h1_hdeaders_case_adjust_deinit); |
| 3122 | return err_code; |
| 3123 | } |
| 3124 | |
| 3125 | |
| 3126 | /* config parser for global "h1-outgoing-header-case-adjust" */ |
| 3127 | static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx, |
| 3128 | struct proxy *defpx, const char *file, int line, |
| 3129 | char **err) |
| 3130 | { |
| 3131 | if (too_many_args(2, args, err, NULL)) |
| 3132 | return -1; |
| 3133 | if (!*(args[1]) || !*(args[2])) { |
| 3134 | memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]); |
| 3135 | return -1; |
| 3136 | } |
| 3137 | return add_hdr_case_adjust(args[1], args[2], err); |
| 3138 | } |
| 3139 | |
| 3140 | /* config parser for global "h1-outgoing-headers-case-adjust-file" */ |
| 3141 | static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx, |
| 3142 | struct proxy *defpx, const char *file, int line, |
| 3143 | char **err) |
| 3144 | { |
| 3145 | if (too_many_args(1, args, err, NULL)) |
| 3146 | return -1; |
| 3147 | if (!*(args[1])) { |
| 3148 | memprintf(err, "'%s' expects <file> as argument.", args[0]); |
| 3149 | return -1; |
| 3150 | } |
| 3151 | free(hdrs_map.name); |
| 3152 | hdrs_map.name = strdup(args[1]); |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | /* config keyword parsers */ |
| 3158 | static struct cfg_kw_list cfg_kws = {{ }, { |
| 3159 | { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust }, |
| 3160 | { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file }, |
| 3161 | { 0, NULL, NULL }, |
| 3162 | } |
| 3163 | }; |
| 3164 | |
| 3165 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 3166 | REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser); |
| 3167 | |
| 3168 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3169 | /****************************************/ |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 3170 | /* MUX initialization and instantiation */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3171 | /****************************************/ |
| 3172 | |
| 3173 | /* The mux operations */ |
Willy Tarreau | f77a158 | 2019-01-10 10:00:08 +0100 | [diff] [blame] | 3174 | static const struct mux_ops mux_h1_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3175 | .init = h1_init, |
| 3176 | .wake = h1_wake, |
| 3177 | .attach = h1_attach, |
| 3178 | .get_first_cs = h1_get_first_cs, |
| 3179 | .detach = h1_detach, |
| 3180 | .destroy = h1_destroy, |
| 3181 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 3182 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3183 | .rcv_buf = h1_rcv_buf, |
| 3184 | .snd_buf = h1_snd_buf, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 3185 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3186 | .rcv_pipe = h1_rcv_pipe, |
| 3187 | .snd_pipe = h1_snd_pipe, |
| 3188 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3189 | .subscribe = h1_subscribe, |
| 3190 | .unsubscribe = h1_unsubscribe, |
| 3191 | .shutr = h1_shutr, |
| 3192 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3193 | .show_fd = h1_show_fd, |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 3194 | .reset = h1_reset, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3195 | .ctl = h1_ctl, |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3196 | .takeover = h1_takeover, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 3197 | .flags = MX_FL_HTX, |
Willy Tarreau | 0a7a4fb | 2019-05-22 11:36:54 +0200 | [diff] [blame] | 3198 | .name = "H1", |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3199 | }; |
| 3200 | |
| 3201 | |
| 3202 | /* this mux registers default HTX proto */ |
| 3203 | static struct mux_proto_list mux_proto_htx = |
Christopher Faulet | c985f6c | 2019-07-15 11:42:52 +0200 | [diff] [blame] | 3204 | { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3206 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx); |
| 3207 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3208 | /* |
| 3209 | * Local variables: |
| 3210 | * c-indent-level: 8 |
| 3211 | * c-basic-offset: 8 |
| 3212 | * End: |
| 3213 | */ |