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