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