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