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