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