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