Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2 | * HTTP/1 mux-demux for connections |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 12 | #include <import/ebistree.h> |
Willy Tarreau | 63617db | 2021-10-06 18:23:40 +0200 | [diff] [blame] | 13 | #include <import/ebmbtree.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 14 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 16 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 17 | #include <haproxy/connection.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 18 | #include <haproxy/dynbuf.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 19 | #include <haproxy/h1.h> |
Willy Tarreau | c6fe884 | 2020-06-04 09:00:02 +0200 | [diff] [blame] | 20 | #include <haproxy/h1_htx.h> |
Willy Tarreau | bf07314 | 2020-06-03 12:04:01 +0200 | [diff] [blame] | 21 | #include <haproxy/h2.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 23 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 24 | #include <haproxy/istbuf.h> |
| 25 | #include <haproxy/log.h> |
Willy Tarreau | 551271d | 2020-06-04 08:32:23 +0200 | [diff] [blame] | 26 | #include <haproxy/pipe-t.h> |
Willy Tarreau | adc0240 | 2021-05-08 20:28:54 +0200 | [diff] [blame] | 27 | #include <haproxy/proxy.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 28 | #include <haproxy/session-t.h> |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 29 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 30 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 31 | #include <haproxy/stream.h> |
Willy Tarreau | c6d61d7 | 2020-06-04 19:02:42 +0200 | [diff] [blame] | 32 | #include <haproxy/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 | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 47 | #define H1C_F_IN_SALLOC 0x00000040 /* mux is blocked on lack of stream's request buffer */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 48 | |
Christopher Faulet | 7b109f2 | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 49 | /* Flags indicating the connection state */ |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 50 | #define H1C_F_ST_EMBRYONIC 0x00000100 /* Set when a H1 stream with no stream connector is attached to the connection */ |
| 51 | #define H1C_F_ST_ATTACHED 0x00000200 /* Set when a H1 stream with a stream connector is attached to the connection (may be not READY) */ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 52 | #define H1C_F_ST_IDLE 0x00000400 /* connection is idle and may be reused |
| 53 | * (exclusive to all H1C_F_ST flags and never set when an h1s is attached) */ |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 54 | #define H1C_F_ST_ERROR 0x00000800 /* connection must be closed ASAP because an error occurred (stream connector may still be attached) */ |
| 55 | #define H1C_F_ST_SHUTDOWN 0x00001000 /* connection must be shut down ASAP flushing output first (stream connector may still be attached) */ |
| 56 | #define H1C_F_ST_READY 0x00002000 /* Set in ATTACHED state with a READY stream connector. A stream connector is not ready when |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 57 | * a TCP>H1 upgrade is in progress Thus this flag is only set if ATTACHED is also set */ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 58 | #define H1C_F_ST_ALIVE (H1C_F_ST_IDLE|H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED) |
Christopher Faulet | 7530830 | 2021-11-15 14:51:37 +0100 | [diff] [blame] | 59 | #define H1C_F_ST_SILENT_SHUT 0x00004000 /* silent (or dirty) shutdown must be performed (implied ST_SHUTDOWN) */ |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 60 | /* 0x00008000 unused */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 61 | |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 62 | #define H1C_F_WANT_SPLICE 0x00010000 /* Don't read into a buffer because we want to use or we are using splicing */ |
| 63 | #define H1C_F_ERR_PENDING 0x00020000 /* Send an error and close the connection ASAP (implies H1C_F_ST_ERROR) */ |
| 64 | #define H1C_F_WAIT_NEXT_REQ 0x00040000 /* waiting for the next request to start, use keep-alive timeout */ |
| 65 | #define H1C_F_UPG_H2C 0x00080000 /* set if an upgrade to h2 should be done */ |
| 66 | #define H1C_F_CO_MSG_MORE 0x00100000 /* set if CO_SFL_MSG_MORE must be set when calling xprt->snd_buf() */ |
| 67 | #define H1C_F_CO_STREAMER 0x00200000 /* set if CO_SFL_STREAMER must be set when calling xprt->snd_buf() */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 69 | /* 0x00400000 - 0x40000000 unusued*/ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 70 | #define H1C_F_IS_BACK 0x80000000 /* Set on outgoing connection */ |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 71 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 72 | /* |
| 73 | * H1 Stream flags (32 bits) |
| 74 | */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 75 | #define H1S_F_NONE 0x00000000 |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 76 | |
| 77 | #define H1S_F_RX_BLK 0x00100000 /* Don't process more input data, waiting sync with output side */ |
| 78 | #define H1S_F_TX_BLK 0x00200000 /* Don't process more output data, waiting sync with input side */ |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 79 | #define H1S_F_RX_CONGESTED 0x00000004 /* Cannot process input data RX path is congested (waiting for more space in channel's buffer) */ |
| 80 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 81 | #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] | 82 | #define H1S_F_WANT_KAL 0x00000010 |
| 83 | #define H1S_F_WANT_TUN 0x00000020 |
| 84 | #define H1S_F_WANT_CLO 0x00000040 |
| 85 | #define H1S_F_WANT_MSK 0x00000070 |
| 86 | #define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */ |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 87 | #define H1S_F_BODYLESS_RESP 0x00000100 /* Bodyless response message */ |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 88 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 89 | /* 0x00000200 unused */ |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 90 | #define H1S_F_NOT_IMPL_ERROR 0x00000400 /* Set when a feature is not implemented during the message parsing */ |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 91 | #define H1S_F_PARSING_ERROR 0x00000800 /* Set when an error occurred during the message parsing */ |
| 92 | #define H1S_F_PROCESSING_ERROR 0x00001000 /* Set when an error occurred during the message xfer */ |
| 93 | #define H1S_F_ERROR 0x00001800 /* stream error mask */ |
| 94 | |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 95 | #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] | 96 | #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] | 97 | |
| 98 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 99 | struct h1c { |
| 100 | struct connection *conn; |
| 101 | struct proxy *px; |
| 102 | uint32_t flags; /* Connection flags: H1C_F_* */ |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 103 | unsigned int errcode; /* Status code when an error occurred at the H1 connection level */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 104 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 105 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
| 106 | |
| 107 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 108 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 109 | |
| 110 | struct h1s *h1s; /* H1 stream descriptor */ |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 111 | struct task *task; /* timeout management task */ |
Christopher Faulet | 563c345 | 2021-11-26 17:37:51 +0100 | [diff] [blame] | 112 | struct h1_counters *px_counters; /* h1 counters attached to proxy */ |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 113 | int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */ |
| 114 | int timeout; /* client/server timeout duration */ |
| 115 | int shut_timeout; /* client-fin/server-fin timeout duration */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | /* H1 stream descriptor */ |
| 119 | struct h1s { |
| 120 | struct h1c *h1c; |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 121 | struct sedesc *endp; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 122 | uint32_t flags; /* Connection flags: H1S_F_* */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 124 | struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 125 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 126 | struct session *sess; /* Associated session */ |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 127 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 128 | struct h1m req; |
| 129 | struct h1m res; |
| 130 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 131 | enum http_meth_t meth; /* HTTP request method */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 132 | uint16_t status; /* HTTP response status */ |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 133 | |
| 134 | char ws_key[25]; /* websocket handshake key */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 137 | /* Map of headers used to convert outgoing headers */ |
| 138 | struct h1_hdrs_map { |
| 139 | char *name; |
| 140 | struct eb_root map; |
| 141 | }; |
| 142 | |
| 143 | /* An entry in a headers map */ |
| 144 | struct h1_hdr_entry { |
| 145 | struct ist name; |
| 146 | struct ebpt_node node; |
| 147 | }; |
| 148 | |
| 149 | /* Declare the headers map */ |
| 150 | static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT }; |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 151 | static int accept_payload_with_any_method = 0; |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 152 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 153 | /* trace source and events */ |
| 154 | static void h1_trace(enum trace_level level, uint64_t mask, |
| 155 | const struct trace_source *src, |
| 156 | const struct ist where, const struct ist func, |
| 157 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 158 | |
| 159 | /* The event representation is split like this : |
| 160 | * h1c - internal H1 connection |
| 161 | * h1s - internal H1 stream |
| 162 | * strm - application layer |
| 163 | * rx - data receipt |
| 164 | * tx - data transmission |
| 165 | * |
| 166 | */ |
| 167 | static const struct trace_event h1_trace_events[] = { |
| 168 | #define H1_EV_H1C_NEW (1ULL << 0) |
| 169 | { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" }, |
| 170 | #define H1_EV_H1C_RECV (1ULL << 1) |
| 171 | { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" }, |
| 172 | #define H1_EV_H1C_SEND (1ULL << 2) |
| 173 | { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" }, |
| 174 | #define H1_EV_H1C_BLK (1ULL << 3) |
| 175 | { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" }, |
| 176 | #define H1_EV_H1C_WAKE (1ULL << 4) |
| 177 | { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" }, |
| 178 | #define H1_EV_H1C_END (1ULL << 5) |
| 179 | { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" }, |
| 180 | #define H1_EV_H1C_ERR (1ULL << 6) |
| 181 | { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" }, |
| 182 | |
| 183 | #define H1_EV_RX_DATA (1ULL << 7) |
| 184 | { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" }, |
| 185 | #define H1_EV_RX_EOI (1ULL << 8) |
| 186 | { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" }, |
| 187 | #define H1_EV_RX_HDRS (1ULL << 9) |
| 188 | { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" }, |
| 189 | #define H1_EV_RX_BODY (1ULL << 10) |
| 190 | { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" }, |
| 191 | #define H1_EV_RX_TLRS (1ULL << 11) |
| 192 | { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" }, |
| 193 | |
| 194 | #define H1_EV_TX_DATA (1ULL << 12) |
| 195 | { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" }, |
| 196 | #define H1_EV_TX_EOI (1ULL << 13) |
| 197 | { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" }, |
| 198 | #define H1_EV_TX_HDRS (1ULL << 14) |
| 199 | { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" }, |
| 200 | #define H1_EV_TX_BODY (1ULL << 15) |
| 201 | { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" }, |
| 202 | #define H1_EV_TX_TLRS (1ULL << 16) |
| 203 | { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" }, |
| 204 | |
| 205 | #define H1_EV_H1S_NEW (1ULL << 17) |
| 206 | { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" }, |
| 207 | #define H1_EV_H1S_BLK (1ULL << 18) |
| 208 | { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" }, |
| 209 | #define H1_EV_H1S_END (1ULL << 19) |
| 210 | { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" }, |
| 211 | #define H1_EV_H1S_ERR (1ULL << 20) |
| 212 | { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" }, |
| 213 | |
| 214 | #define H1_EV_STRM_NEW (1ULL << 21) |
| 215 | { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 216 | #define H1_EV_STRM_RECV (1ULL << 22) |
| 217 | { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 218 | #define H1_EV_STRM_SEND (1ULL << 23) |
| 219 | { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 220 | #define H1_EV_STRM_WAKE (1ULL << 24) |
| 221 | { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" }, |
| 222 | #define H1_EV_STRM_SHUT (1ULL << 25) |
| 223 | { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 224 | #define H1_EV_STRM_END (1ULL << 26) |
| 225 | { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 226 | #define H1_EV_STRM_ERR (1ULL << 27) |
| 227 | { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 228 | |
| 229 | { } |
| 230 | }; |
| 231 | |
| 232 | static const struct name_desc h1_trace_lockon_args[4] = { |
| 233 | /* arg1 */ { /* already used by the connection */ }, |
| 234 | /* arg2 */ { .name="h1s", .desc="H1 stream" }, |
| 235 | /* arg3 */ { }, |
| 236 | /* arg4 */ { } |
| 237 | }; |
| 238 | |
| 239 | static const struct name_desc h1_trace_decoding[] = { |
| 240 | #define H1_VERB_CLEAN 1 |
| 241 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 242 | #define H1_VERB_MINIMAL 2 |
| 243 | { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" }, |
| 244 | #define H1_VERB_SIMPLE 3 |
| 245 | { .name="simple", .desc="add request/response status line or htx info when available" }, |
| 246 | #define H1_VERB_ADVANCED 4 |
| 247 | { .name="advanced", .desc="add header fields or frame decoding when available" }, |
| 248 | #define H1_VERB_COMPLETE 5 |
| 249 | { .name="complete", .desc="add full data dump when available" }, |
| 250 | { /* end */ } |
| 251 | }; |
| 252 | |
Willy Tarreau | 6eb3d37 | 2021-04-10 19:29:26 +0200 | [diff] [blame] | 253 | static struct trace_source trace_h1 __read_mostly = { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 254 | .name = IST("h1"), |
| 255 | .desc = "HTTP/1 multiplexer", |
| 256 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 257 | .default_cb = h1_trace, |
| 258 | .known_events = h1_trace_events, |
| 259 | .lockon_args = h1_trace_lockon_args, |
| 260 | .decoding = h1_trace_decoding, |
| 261 | .report_events = ~0, // report everything by default |
| 262 | }; |
| 263 | |
| 264 | #define TRACE_SOURCE &trace_h1 |
| 265 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 266 | |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 267 | |
| 268 | /* h1 stats module */ |
| 269 | enum { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 270 | H1_ST_OPEN_CONN, |
| 271 | H1_ST_OPEN_STREAM, |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 272 | H1_ST_TOTAL_CONN, |
| 273 | H1_ST_TOTAL_STREAM, |
| 274 | |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 275 | H1_ST_BYTES_IN, |
| 276 | H1_ST_BYTES_OUT, |
| 277 | #if defined(USE_LINUX_SPLICE) |
| 278 | H1_ST_SPLICED_BYTES_IN, |
| 279 | H1_ST_SPLICED_BYTES_OUT, |
| 280 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 281 | H1_STATS_COUNT /* must be the last member of the enum */ |
| 282 | }; |
| 283 | |
| 284 | |
| 285 | static struct name_desc h1_stats[] = { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 286 | [H1_ST_OPEN_CONN] = { .name = "h1_open_connections", |
| 287 | .desc = "Count of currently open connections" }, |
| 288 | [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams", |
| 289 | .desc = "Count of currently open streams" }, |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 290 | [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections", |
| 291 | .desc = "Total number of connections" }, |
| 292 | [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams", |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 293 | .desc = "Total number of streams" }, |
| 294 | |
| 295 | [H1_ST_BYTES_IN] = { .name = "h1_bytes_in", |
| 296 | .desc = "Total number of bytes received" }, |
| 297 | [H1_ST_BYTES_OUT] = { .name = "h1_bytes_out", |
| 298 | .desc = "Total number of bytes send" }, |
| 299 | #if defined(USE_LINUX_SPLICE) |
| 300 | [H1_ST_SPLICED_BYTES_IN] = { .name = "h1_spliced_bytes_in", |
| 301 | .desc = "Total number of bytes received using kernel splicing" }, |
| 302 | [H1_ST_SPLICED_BYTES_OUT] = { .name = "h1_spliced_bytes_out", |
| 303 | .desc = "Total number of bytes sendusing kernel splicing" }, |
| 304 | #endif |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 305 | |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | static struct h1_counters { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 309 | long long open_conns; /* count of currently open connections */ |
| 310 | long long open_streams; /* count of currently open streams */ |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 311 | long long total_conns; /* total number of connections */ |
| 312 | long long total_streams; /* total number of streams */ |
| 313 | |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 314 | long long bytes_in; /* number of bytes received */ |
| 315 | long long bytes_out; /* number of bytes sent */ |
| 316 | #if defined(USE_LINUX_SPLICE) |
| 317 | long long spliced_bytes_in; /* number of bytes received using kernel splicing */ |
| 318 | long long spliced_bytes_out; /* number of bytes sent using kernel splicing */ |
| 319 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 320 | } h1_counters; |
| 321 | |
| 322 | static void h1_fill_stats(void *data, struct field *stats) |
| 323 | { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 324 | struct h1_counters *counters = data; |
| 325 | |
| 326 | stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns); |
| 327 | stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 328 | stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns); |
| 329 | stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams); |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 330 | |
| 331 | stats[H1_ST_BYTES_IN] = mkf_u64(FN_COUNTER, counters->bytes_in); |
| 332 | stats[H1_ST_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->bytes_out); |
| 333 | #if defined(USE_LINUX_SPLICE) |
| 334 | stats[H1_ST_SPLICED_BYTES_IN] = mkf_u64(FN_COUNTER, counters->spliced_bytes_in); |
| 335 | stats[H1_ST_SPLICED_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->spliced_bytes_out); |
| 336 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static struct stats_module h1_stats_module = { |
| 340 | .name = "h1", |
| 341 | .fill_stats = h1_fill_stats, |
| 342 | .stats = h1_stats, |
| 343 | .stats_count = H1_STATS_COUNT, |
| 344 | .counters = &h1_counters, |
| 345 | .counters_size = sizeof(h1_counters), |
| 346 | .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE), |
| 347 | .clearable = 1, |
| 348 | }; |
| 349 | |
| 350 | INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module); |
| 351 | |
| 352 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 353 | /* the h1c and h1s pools */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 354 | DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c)); |
| 355 | DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 356 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 357 | static int h1_recv(struct h1c *h1c); |
| 358 | static int h1_send(struct h1c *h1c); |
| 359 | static int h1_process(struct h1c *h1c); |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 360 | /* h1_io_cb is exported to see it resolved in "show fd" */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 361 | struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state); |
| 362 | struct task *h1_timeout_task(struct task *t, void *context, unsigned int state); |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 363 | static void h1_shutw_conn(struct connection *conn); |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 364 | static void h1_wake_stream_for_recv(struct h1s *h1s); |
| 365 | static void h1_wake_stream_for_send(struct h1s *h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 367 | /* returns the stconn associated to the H1 stream */ |
| 368 | static forceinline struct stconn *h1s_sc(const struct h1s *h1s) |
| 369 | { |
Willy Tarreau | c105492 | 2022-05-18 07:43:52 +0200 | [diff] [blame] | 370 | return h1s->endp->sc; |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 373 | /* the H1 traces always expect that arg1, if non-null, is of type connection |
| 374 | * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and |
| 375 | * that arg3, if non-null, is a htx for rx/tx headers. |
| 376 | */ |
| 377 | static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 378 | const struct ist where, const struct ist func, |
| 379 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 380 | { |
| 381 | const struct connection *conn = a1; |
| 382 | const struct h1c *h1c = conn ? conn->ctx : NULL; |
| 383 | const struct h1s *h1s = a2; |
| 384 | const struct htx *htx = a3; |
| 385 | const size_t *val = a4; |
| 386 | |
| 387 | if (!h1c) |
| 388 | h1c = (h1s ? h1s->h1c : NULL); |
| 389 | |
| 390 | if (!h1c || src->verbosity < H1_VERB_CLEAN) |
| 391 | return; |
| 392 | |
| 393 | /* Display frontend/backend info by default */ |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 394 | chunk_appendf(&trace_buf, " : [%c]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F')); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 395 | |
| 396 | /* Display request and response states if h1s is defined */ |
Christopher Faulet | 6580f28 | 2021-11-26 17:31:35 +0100 | [diff] [blame] | 397 | if (h1s) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 398 | chunk_appendf(&trace_buf, " [%s, %s]", |
| 399 | h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state)); |
| 400 | |
Christopher Faulet | 6580f28 | 2021-11-26 17:31:35 +0100 | [diff] [blame] | 401 | if (src->verbosity > H1_VERB_SIMPLE) { |
| 402 | chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)", |
| 403 | h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len); |
| 404 | chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)", |
| 405 | h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len); |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 410 | if (src->verbosity == H1_VERB_CLEAN) |
| 411 | return; |
| 412 | |
| 413 | /* Display the value to the 4th argument (level > STATE) */ |
| 414 | if (src->level > TRACE_LEVEL_STATE && val) |
Willy Tarreau | e18f53e | 2019-11-27 15:41:31 +0100 | [diff] [blame] | 415 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 416 | |
| 417 | /* Display status-line if possible (verbosity > MINIMAL) */ |
| 418 | if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) { |
| 419 | const struct htx_blk *blk = htx_get_head_blk(htx); |
| 420 | const struct htx_sl *sl = htx_get_blk_ptr(htx, blk); |
| 421 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 422 | |
| 423 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) |
| 424 | chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"", |
| 425 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 426 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 427 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
| 428 | } |
| 429 | |
| 430 | /* Display h1c info and, if defined, h1s info (pointer + flags) */ |
| 431 | chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags); |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 432 | if (h1c->conn) |
| 433 | chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags); |
| 434 | if (h1s) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 435 | chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags); |
Christopher Faulet | 186354b | 2022-04-13 12:09:36 +0200 | [diff] [blame] | 436 | if (h1s->endp) |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame] | 437 | chunk_appendf(&trace_buf, " endp=%p(0x%08x)", h1s->endp, se_fl_get(h1s->endp)); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 438 | if (h1s->endp && h1s_sc(h1s)) |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 439 | chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags); |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 440 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 441 | |
| 442 | if (src->verbosity == H1_VERB_MINIMAL) |
| 443 | return; |
| 444 | |
| 445 | /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */ |
| 446 | if (src->level > TRACE_LEVEL_USER) { |
| 447 | if (src->verbosity == H1_VERB_COMPLETE || |
| 448 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV)))) |
| 449 | chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u", |
| 450 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 451 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf)); |
| 452 | if (src->verbosity == H1_VERB_COMPLETE || |
| 453 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND)))) |
| 454 | chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u", |
| 455 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 456 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 457 | } |
| 458 | |
| 459 | /* Display htx info if defined (level > USER) */ |
| 460 | if (src->level > TRACE_LEVEL_USER && htx) { |
| 461 | int full = 0; |
| 462 | |
| 463 | /* Full htx info (level > STATE && verbosity > SIMPLE) */ |
| 464 | if (src->level > TRACE_LEVEL_STATE) { |
| 465 | if (src->verbosity == H1_VERB_COMPLETE) |
| 466 | full = 1; |
| 467 | else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS))) |
| 468 | full = 1; |
| 469 | } |
| 470 | |
| 471 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 472 | htx_dump(&trace_buf, htx, full); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 477 | /*****************************************************/ |
| 478 | /* functions below are for dynamic buffer management */ |
| 479 | /*****************************************************/ |
| 480 | /* |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 481 | * Indicates whether or not we may receive data. The rules are the following : |
| 482 | * - if an error or a shutdown for reads was detected on the connection we |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 483 | * must not attempt to receive |
| 484 | * - if we are waiting for the connection establishment, we must not attempt |
| 485 | * to receive |
| 486 | * - if an error was detected on the stream we must not attempt to receive |
| 487 | * - if reads are explicitly disabled, we must not attempt to receive |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 488 | * - if the input buffer failed to be allocated or is full , we must not try |
| 489 | * to receive |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 490 | * - if the mux is not blocked on an input condition, we may attempt to receive |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 491 | * - otherwise must may not attempt to receive |
| 492 | */ |
| 493 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 494 | { |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 495 | if (h1c->flags & H1C_F_ST_ERROR) { |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 496 | 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] | 497 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 498 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 499 | |
Willy Tarreau | 2febb84 | 2020-07-31 09:15:43 +0200 | [diff] [blame] | 500 | if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 501 | TRACE_DEVEL("recv not allowed because of (error|read0|waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 502 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 503 | } |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 504 | |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 505 | if (h1c->h1s && (h1c->h1s->flags & H1S_F_ERROR)) { |
| 506 | TRACE_DEVEL("recv not allowed because of error on h1s", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
| 507 | return 0; |
| 508 | } |
| 509 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 510 | if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 511 | return 1; |
| 512 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 513 | 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] | 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 519 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 520 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 521 | * impossible to wake up and we prefer to be woken up later. |
| 522 | */ |
| 523 | static int h1_buf_available(void *target) |
| 524 | { |
| 525 | struct h1c *h1c = target; |
| 526 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 527 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 528 | 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] | 529 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 530 | if (h1_recv_allowed(h1c)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 531 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 532 | return 1; |
| 533 | } |
| 534 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 535 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 536 | 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] | 537 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 538 | if (h1c->h1s) |
| 539 | h1_wake_stream_for_send(h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 543 | if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) { |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 544 | TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn); |
| 545 | h1c->flags &= ~H1C_F_IN_SALLOC; |
| 546 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 547 | return 1; |
| 548 | } |
| 549 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 555 | */ |
| 556 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 557 | { |
| 558 | struct buffer *buf = NULL; |
| 559 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 560 | if (likely(!LIST_INLIST(&h1c->buf_wait.list)) && |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 561 | unlikely((buf = b_alloc(bptr)) == NULL)) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 562 | h1c->buf_wait.target = h1c; |
| 563 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 564 | LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 565 | } |
| 566 | return buf; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 571 | * wait queue. |
| 572 | */ |
| 573 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 574 | { |
| 575 | if (bptr->size) { |
| 576 | b_free(bptr); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 577 | offer_buffers(h1c->buf_wait.target, 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 581 | /* returns the number of streams in use on a connection to figure if it's idle |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 582 | * or not. We rely on H1C_F_ST_IDLE to know if the connection is in-use or |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 583 | * not. This flag is only set when no H1S is attached and when the previous |
| 584 | * 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] | 585 | */ |
| 586 | static int h1_used_streams(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 587 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 588 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 589 | |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 590 | return ((h1c->flags & H1C_F_ST_IDLE) ? 0 : 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 593 | /* returns the number of streams still available on a connection */ |
| 594 | static int h1_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 595 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 596 | return 1 - h1_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 597 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 598 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 599 | /* Refresh the h1c task timeout if necessary */ |
| 600 | static void h1_refresh_timeout(struct h1c *h1c) |
| 601 | { |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 602 | int is_idle_conn = 0; |
| 603 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 604 | if (h1c->task) { |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 605 | if (!(h1c->flags & H1C_F_ST_ALIVE) || (h1c->flags & H1C_F_ST_SHUTDOWN)) { |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 606 | /* half-closed or dead connections : switch to clientfin/serverfin |
| 607 | * timeouts so that we don't hang too long on clients that have |
| 608 | * gone away (especially in tunnel mode). |
Willy Tarreau | 4313d5a | 2020-09-08 15:40:57 +0200 | [diff] [blame] | 609 | */ |
| 610 | h1c->task->expire = tick_add(now_ms, h1c->shut_timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 611 | TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 612 | is_idle_conn = 1; |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 613 | } |
| 614 | else if (b_data(&h1c->obuf)) { |
| 615 | /* connection with pending outgoing data, need a timeout (server or client). */ |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 616 | h1c->task->expire = tick_add(now_ms, h1c->timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 617 | TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 618 | } |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 619 | else if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_READY))) { |
| 620 | /* front connections waiting for a fully usable stream need a timeout. */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 621 | h1c->task->expire = tick_add(now_ms, h1c->timeout); |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 622 | TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 623 | /* A frontend connection not yet ready could be treated the same way as an idle |
| 624 | * one in case of soft-close. |
| 625 | */ |
| 626 | is_idle_conn = 1; |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 627 | } |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 628 | else { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 629 | /* alive back connections of front connections with a stream connector attached */ |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 630 | h1c->task->expire = TICK_ETERNITY; |
| 631 | TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with a CS)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 632 | } |
| 633 | |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 634 | /* Finally set the idle expiration date if shorter */ |
| 635 | h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 636 | |
| 637 | if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) && |
| 638 | is_idle_conn && tick_isset(global.close_spread_end)) { |
| 639 | /* If a soft-stop is in progress and a close-spread-time |
| 640 | * is set, we want to spread idle connection closing roughly |
| 641 | * evenly across the defined window. This should only |
| 642 | * act on idle frontend connections. |
| 643 | * If the window end is already in the past, we wake the |
| 644 | * timeout task up immediately so that it can be closed. |
| 645 | */ |
| 646 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 647 | if (remaining_window) { |
| 648 | /* We don't need to reset the expire if it would |
| 649 | * already happen before the close window end. |
| 650 | */ |
| 651 | if (tick_is_le(global.close_spread_end, h1c->task->expire)) { |
| 652 | /* Set an expire value shorter than the current value |
| 653 | * because the close spread window end comes earlier. |
| 654 | */ |
| 655 | h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window)); |
| 656 | TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 657 | } |
| 658 | } |
| 659 | else { |
| 660 | /* We are past the soft close window end, wake the timeout |
| 661 | * task up immediately. |
| 662 | */ |
| 663 | task_wakeup(h1c->task, TASK_WOKEN_TIMER); |
| 664 | } |
| 665 | } |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 666 | TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire}); |
| 667 | task_queue(h1c->task); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 668 | } |
| 669 | } |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 670 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 671 | static void h1_set_idle_expiration(struct h1c *h1c) |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 672 | { |
| 673 | if (h1c->flags & H1C_F_IS_BACK || !h1c->task) { |
| 674 | TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn); |
| 675 | h1c->idle_exp = TICK_ETERNITY; |
| 676 | return; |
| 677 | } |
| 678 | |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 679 | if (h1c->flags & H1C_F_ST_IDLE) { |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 680 | if (!tick_isset(h1c->idle_exp)) { |
| 681 | if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */ |
| 682 | !b_data(&h1c->ibuf) && /* No input data */ |
| 683 | tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */ |
| 684 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka); |
| 685 | TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 686 | } |
| 687 | else { |
| 688 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq); |
| 689 | TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 690 | } |
| 691 | } |
| 692 | } |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 693 | else if ((h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY)) { |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 694 | if (!tick_isset(h1c->idle_exp)) { |
| 695 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq); |
| 696 | TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 697 | } |
| 698 | } |
| 699 | else { // CS_ATTACHED or SHUTDOWN |
| 700 | h1c->idle_exp = TICK_ETERNITY; |
| 701 | TRACE_DEVEL("unset idle expiration (attached || shutdown)", H1_EV_H1C_RECV, h1c->conn); |
| 702 | } |
| 703 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 704 | /*****************************************************************/ |
| 705 | /* functions below are dedicated to the mux setup and management */ |
| 706 | /*****************************************************************/ |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 707 | |
| 708 | /* returns non-zero if there are input data pending for stream h1s. */ |
| 709 | static inline size_t h1s_data_pending(const struct h1s *h1s) |
| 710 | { |
| 711 | const struct h1m *h1m; |
| 712 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 713 | h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 714 | return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf)); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 717 | /* Creates a new stream connector and the associate stream. <input> is used as input |
Christopher Faulet | 16df178 | 2020-12-04 16:47:41 +0100 | [diff] [blame] | 718 | * buffer for the stream. On success, it is transferred to the stream and the |
| 719 | * mux is no longer responsible of it. On error, <input> is unchanged, thus the |
| 720 | * mux must still take care of it. However, there is nothing special to do |
| 721 | * because, on success, <input> is updated to points on BUF_NULL. Thus, calling |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 722 | * b_free() on it is always safe. This function returns the stream connector on |
Christopher Faulet | 16df178 | 2020-12-04 16:47:41 +0100 | [diff] [blame] | 723 | * success or NULL on error. */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 724 | static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 725 | { |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 726 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 727 | |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 728 | TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 729 | |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 730 | if (h1s->flags & H1S_F_NOT_FIRST) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 731 | se_fl_set(h1s->endp, SE_FL_NOT_FIRST); |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 732 | if (h1s->req.flags & H1_MF_UPG_WEBSOCKET) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 733 | se_fl_set(h1s->endp, SE_FL_WEBSOCKET); |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 734 | |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 735 | if (!sc_new_from_endp(h1s->endp, h1c->conn->owner, input)) { |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 736 | TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 737 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 738 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 739 | |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 740 | HA_ATOMIC_INC(&h1c->px_counters->open_streams); |
| 741 | HA_ATOMIC_INC(&h1c->px_counters->total_streams); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 742 | |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 743 | h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY; |
| 744 | TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 745 | return h1s_sc(h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 746 | |
Christopher Faulet | 91449b0 | 2022-03-22 18:45:55 +0100 | [diff] [blame] | 747 | err: |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 748 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 749 | return NULL; |
| 750 | } |
| 751 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 752 | static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input) |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 753 | { |
| 754 | TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
| 755 | |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 756 | if (stream_upgrade_from_cs(h1s_sc(h1s), input) < 0) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 757 | TRACE_ERROR("stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 758 | goto err; |
| 759 | } |
| 760 | |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 761 | h1s->h1c->flags |= H1C_F_ST_READY; |
| 762 | TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 763 | return h1s_sc(h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 764 | |
| 765 | err: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 766 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 767 | return NULL; |
| 768 | } |
| 769 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 770 | static struct h1s *h1s_new(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 771 | { |
| 772 | struct h1s *h1s; |
| 773 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 774 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 775 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 776 | h1s = pool_alloc(pool_head_h1s); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 777 | if (!h1s) { |
| 778 | TRACE_ERROR("H1S allocation failure", 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] | 779 | goto fail; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 780 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 781 | h1s->h1c = h1c; |
| 782 | h1c->h1s = h1s; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 783 | h1s->sess = NULL; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 784 | h1s->endp = NULL; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 785 | h1s->flags = H1S_F_WANT_KAL; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 786 | h1s->subs = NULL; |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 787 | h1s->rxbuf = BUF_NULL; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 788 | memset(h1s->ws_key, 0, sizeof(h1s->ws_key)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 789 | |
| 790 | h1m_init_req(&h1s->req); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 791 | h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 792 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 793 | h1m_init_res(&h1s->res); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 794 | h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 795 | |
| 796 | h1s->status = 0; |
| 797 | h1s->meth = HTTP_METH_OTHER; |
| 798 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 799 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 800 | h1s->flags |= H1S_F_NOT_FIRST; |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 801 | h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_EMBRYONIC; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 802 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 803 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 804 | return h1s; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 805 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 806 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 807 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 808 | return NULL; |
| 809 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 810 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 811 | static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess) |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 812 | { |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 813 | struct h1s *h1s; |
| 814 | |
| 815 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 816 | |
| 817 | h1s = h1s_new(h1c); |
| 818 | if (!h1s) |
| 819 | goto fail; |
| 820 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 821 | if (sc) { |
| 822 | if (sc_attach_mux(sc, h1s, h1c->conn) < 0) |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 823 | goto fail; |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 824 | h1s->endp = sc->sedesc; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 825 | } |
| 826 | else { |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 827 | h1s->endp = sedesc_new(); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 828 | if (!h1s->endp) |
| 829 | goto fail; |
Willy Tarreau | 65d0597 | 2022-05-16 17:29:42 +0200 | [diff] [blame] | 830 | h1s->endp->se = h1s; |
Willy Tarreau | 24d15b1 | 2022-05-16 17:17:16 +0200 | [diff] [blame] | 831 | h1s->endp->conn = h1c->conn; |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 832 | se_fl_set(h1s->endp, SE_FL_T_MUX | SE_FL_ORPHAN); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 833 | } |
| 834 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 835 | h1s->sess = sess; |
| 836 | |
| 837 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 838 | h1s->req.err_pos = -1; |
| 839 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 840 | h1c->idle_exp = TICK_ETERNITY; |
| 841 | h1_set_idle_expiration(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 842 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 843 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 844 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 845 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 846 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 847 | pool_free(pool_head_h1s, h1s); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 848 | return NULL; |
| 849 | } |
| 850 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 851 | static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess) |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 852 | { |
| 853 | struct h1s *h1s; |
| 854 | |
| 855 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 856 | |
| 857 | h1s = h1s_new(h1c); |
| 858 | if (!h1s) |
| 859 | goto fail; |
| 860 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 861 | if (sc_attach_mux(sc, h1s, h1c->conn) < 0) |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 862 | goto fail; |
| 863 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 864 | h1s->flags |= H1S_F_RX_BLK; |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 865 | h1s->endp = sc->sedesc; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 866 | h1s->sess = sess; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 867 | |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 868 | h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED | H1C_F_ST_READY; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 869 | |
| 870 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 871 | h1s->res.err_pos = -1; |
| 872 | |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 873 | HA_ATOMIC_INC(&h1c->px_counters->open_streams); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 874 | HA_ATOMIC_INC(&h1c->px_counters->total_streams); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 875 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 876 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 877 | return h1s; |
| 878 | |
| 879 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 880 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 881 | pool_free(pool_head_h1s, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 882 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | static void h1s_destroy(struct h1s *h1s) |
| 886 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 887 | if (h1s) { |
| 888 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 889 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 890 | TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 891 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 892 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 893 | if (h1s->subs) |
| 894 | h1s->subs->events = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 895 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 896 | h1_release_buf(h1c, &h1s->rxbuf); |
| 897 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 898 | h1c->flags &= ~(H1C_F_WANT_SPLICE| |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 899 | H1C_F_ST_EMBRYONIC|H1C_F_ST_ATTACHED|H1C_F_ST_READY| |
Christopher Faulet | 295b8d1 | 2020-09-30 17:14:55 +0200 | [diff] [blame] | 900 | H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC| |
| 901 | H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 902 | if (h1s->flags & H1S_F_ERROR) { |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 903 | h1c->flags |= H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 904 | TRACE_ERROR("h1s on error, set error on h1c", H1_EV_H1S_END|H1_EV_H1C_ERR, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 905 | } |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 906 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 907 | if (!(h1c->flags & (H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN)) && /* No error/shutdown on h1c */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 908 | !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 909 | (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 910 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 911 | h1c->flags |= (H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ); |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 912 | TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s); |
| 913 | } |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 914 | else { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 915 | TRACE_STATE("set shudown on h1c", H1_EV_H1S_END, h1c->conn, h1s); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 916 | h1c->flags |= H1C_F_ST_SHUTDOWN; |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 917 | } |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 918 | |
| 919 | HA_ATOMIC_DEC(&h1c->px_counters->open_streams); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 920 | BUG_ON(h1s->endp && !se_fl_test(h1s->endp, SE_FL_ORPHAN)); |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 921 | sedesc_free(h1s->endp); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 922 | pool_free(pool_head_h1s, h1s); |
| 923 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 927 | * Initialize the mux once it's attached. It is expected that conn->ctx points |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 928 | * to the existing stream connector (for outgoing connections or for incoming |
| 929 | * ones during a mux upgrade) or NULL (for incoming ones during the connection |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 930 | * establishment). <input> is always used as Input buffer and may contain |
| 931 | * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on |
| 932 | * error. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 933 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 934 | static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess, |
| 935 | struct buffer *input) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 936 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 937 | struct h1c *h1c; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 938 | struct task *t = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 939 | void *conn_ctx = conn->ctx; |
| 940 | |
| 941 | TRACE_ENTER(H1_EV_H1C_NEW); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 942 | |
| 943 | h1c = pool_alloc(pool_head_h1c); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 944 | if (!h1c) { |
| 945 | TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 946 | goto fail_h1c; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 947 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 948 | h1c->conn = conn; |
| 949 | h1c->px = proxy; |
| 950 | |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 951 | h1c->flags = H1C_F_ST_IDLE; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 952 | h1c->errcode = 0; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 953 | h1c->ibuf = *input; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 954 | h1c->obuf = BUF_NULL; |
| 955 | h1c->h1s = NULL; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 956 | h1c->task = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 957 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 958 | LIST_INIT(&h1c->buf_wait.list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 959 | h1c->wait_event.tasklet = tasklet_new(); |
| 960 | if (!h1c->wait_event.tasklet) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 961 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 962 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 963 | h1c->wait_event.tasklet->context = h1c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 964 | h1c->wait_event.events = 0; |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 965 | h1c->idle_exp = TICK_ETERNITY; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 966 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 967 | if (conn_is_back(conn)) { |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 968 | h1c->flags |= H1C_F_IS_BACK; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 969 | h1c->shut_timeout = h1c->timeout = proxy->timeout.server; |
| 970 | if (tick_isset(proxy->timeout.serverfin)) |
| 971 | h1c->shut_timeout = proxy->timeout.serverfin; |
Christopher Faulet | 563c345 | 2021-11-26 17:37:51 +0100 | [diff] [blame] | 972 | |
| 973 | h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be, |
| 974 | &h1_stats_module); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 975 | } else { |
| 976 | h1c->shut_timeout = h1c->timeout = proxy->timeout.client; |
| 977 | if (tick_isset(proxy->timeout.clientfin)) |
| 978 | h1c->shut_timeout = proxy->timeout.clientfin; |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 979 | |
Christopher Faulet | 563c345 | 2021-11-26 17:37:51 +0100 | [diff] [blame] | 980 | h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe, |
| 981 | &h1_stats_module); |
| 982 | |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 983 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 984 | &h1c->conn->stopping_list); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 985 | } |
| 986 | if (tick_isset(h1c->timeout)) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 987 | t = task_new_here(); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 988 | if (!t) { |
| 989 | TRACE_ERROR("H1C task allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 990 | goto fail; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 991 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 992 | |
| 993 | h1c->task = t; |
| 994 | t->process = h1_timeout_task; |
| 995 | t->context = h1c; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 996 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 997 | t->expire = tick_add(now_ms, h1c->timeout); |
| 998 | } |
| 999 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 1000 | conn->ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1001 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1002 | if (h1c->flags & H1C_F_IS_BACK) { |
| 1003 | /* Create a new H1S now for backend connection only */ |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 1004 | if (!h1c_bck_stream_new(h1c, conn_ctx, sess)) |
| 1005 | goto fail; |
| 1006 | } |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1007 | else if (conn_ctx) { |
| 1008 | /* Upgraded frontend connection (from TCP) */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1009 | if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner)) |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1010 | goto fail; |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1011 | |
| 1012 | /* Attach the CS but Not ready yet */ |
| 1013 | h1c->flags = (h1c->flags & ~H1C_F_ST_EMBRYONIC) | H1C_F_ST_ATTACHED; |
| 1014 | TRACE_DEVEL("Inherit the CS from TCP connection to perform an upgrade", |
| 1015 | H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s); |
| 1016 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1017 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1018 | if (t) { |
| 1019 | h1_set_idle_expiration(h1c); |
| 1020 | t->expire = tick_first(t->expire, h1c->idle_exp); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1021 | task_queue(t); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1022 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1023 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1024 | /* prepare to read something */ |
Christopher Faulet | d9ee788 | 2021-01-21 17:45:45 +0100 | [diff] [blame] | 1025 | if (b_data(&h1c->ibuf)) |
| 1026 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 1027 | else if (h1_recv_allowed(h1c)) |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 1028 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1029 | |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 1030 | HA_ATOMIC_INC(&h1c->px_counters->open_conns); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 1031 | HA_ATOMIC_INC(&h1c->px_counters->total_conns); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 1032 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1033 | /* mux->wake will be called soon to complete the operation */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1034 | TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1035 | return 0; |
| 1036 | |
| 1037 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1038 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1039 | if (h1c->wait_event.tasklet) |
| 1040 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1041 | pool_free(pool_head_h1c, h1c); |
| 1042 | fail_h1c: |
Willy Tarreau | 3b990fe | 2022-01-12 17:24:26 +0100 | [diff] [blame] | 1043 | if (!conn_is_back(conn)) |
| 1044 | LIST_DEL_INIT(&conn->stopping_list); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1045 | conn->ctx = conn_ctx; // restore saved context |
| 1046 | 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] | 1047 | return -1; |
| 1048 | } |
| 1049 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 1050 | /* release function. This one should be called to free all resources allocated |
| 1051 | * to the mux. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1052 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 1053 | static void h1_release(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1054 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 1055 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1056 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1057 | TRACE_POINT(H1_EV_H1C_END); |
| 1058 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1059 | /* The connection must be aattached to this mux to be released */ |
| 1060 | if (h1c->conn && h1c->conn->ctx == h1c) |
| 1061 | conn = h1c->conn; |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 1062 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1063 | if (conn && h1c->flags & H1C_F_UPG_H2C) { |
| 1064 | TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn); |
| 1065 | /* Make sure we're no longer subscribed to anything */ |
| 1066 | if (h1c->wait_event.events) |
| 1067 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 1068 | h1c->wait_event.events, &h1c->wait_event); |
| 1069 | if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) { |
| 1070 | /* connection successfully upgraded to H2, this |
| 1071 | * mux was already released */ |
| 1072 | return; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1073 | } |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1074 | TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn); |
| 1075 | sess_log(conn->owner); /* Log if the upgrade failed */ |
| 1076 | } |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 1077 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1078 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1079 | if (LIST_INLIST(&h1c->buf_wait.list)) |
| 1080 | LIST_DEL_INIT(&h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1081 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1082 | h1_release_buf(h1c, &h1c->ibuf); |
| 1083 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1084 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1085 | if (h1c->task) { |
| 1086 | h1c->task->context = NULL; |
| 1087 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 1088 | h1c->task = NULL; |
| 1089 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1090 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1091 | if (h1c->wait_event.tasklet) |
| 1092 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 1093 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1094 | h1s_destroy(h1c->h1s); |
| 1095 | if (conn) { |
| 1096 | if (h1c->wait_event.events != 0) |
| 1097 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events, |
| 1098 | &h1c->wait_event); |
| 1099 | h1_shutw_conn(conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1102 | HA_ATOMIC_DEC(&h1c->px_counters->open_conns); |
| 1103 | pool_free(pool_head_h1c, h1c); |
| 1104 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1105 | if (conn) { |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 1106 | if (!conn_is_back(conn)) |
| 1107 | LIST_DEL_INIT(&conn->stopping_list); |
| 1108 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1109 | conn->mux = NULL; |
| 1110 | conn->ctx = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1111 | TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1112 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1113 | conn_stop_tracking(conn); |
| 1114 | conn_full_close(conn); |
| 1115 | if (conn->destroy_cb) |
| 1116 | conn->destroy_cb(conn); |
| 1117 | conn_free(conn); |
| 1118 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /******************************************************/ |
| 1122 | /* functions below are for the H1 protocol processing */ |
| 1123 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1124 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 1125 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1126 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1127 | 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] | 1128 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1129 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1130 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1131 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1132 | (*(p + 5) > '1' || |
| 1133 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 1134 | h1m->flags |= H1_MF_VER_11; |
| 1135 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1136 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1137 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 1138 | * greater or equal to 1.1 |
| 1139 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1140 | 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] | 1141 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1142 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1143 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1144 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1145 | (*(p + 5) > '1' || |
| 1146 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 1147 | h1m->flags |= H1_MF_VER_11; |
| 1148 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1149 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1150 | /* Deduce the connection mode of the client connection, depending on the |
| 1151 | * configuration and the H1 message flags. This function is called twice, the |
| 1152 | * first time when the request is parsed and the second time when the response |
| 1153 | * is parsed. |
| 1154 | */ |
| 1155 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 1156 | { |
| 1157 | struct proxy *fe = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1158 | |
| 1159 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1160 | /* Output direction: second pass */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1161 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1162 | h1s->status == 101) { |
| 1163 | /* Either we've established an explicit tunnel, or we're |
| 1164 | * switching the protocol. In both cases, we're very unlikely to |
| 1165 | * understand the next protocols. We have to switch to tunnel |
| 1166 | * mode, so that we transfer the request and responses then let |
| 1167 | * this protocol pass unmodified. When we later implement |
| 1168 | * specific parsers for such protocols, we'll want to check the |
| 1169 | * Upgrade header which contains information about that protocol |
| 1170 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 1171 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1172 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1173 | 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] | 1174 | } |
| 1175 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 1176 | /* By default the client is in KAL mode. CLOSE mode mean |
| 1177 | * it is imposed by the client itself. So only change |
| 1178 | * KAL mode here. */ |
| 1179 | if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) { |
| 1180 | /* no length known or explicit close => close */ |
| 1181 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1182 | 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] | 1183 | } |
| 1184 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 1185 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1186 | /* no explicit keep-alive and option httpclose => close */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1187 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1188 | 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] | 1189 | } |
| 1190 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1191 | } |
| 1192 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1193 | /* Input direction: first pass */ |
| 1194 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) { |
| 1195 | /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1196 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1197 | 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] | 1198 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1201 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 1202 | * unless a 'close-spread-time' option is set (either to define a |
| 1203 | * soft-close window or to disable active closing (close-spread-time |
| 1204 | * option set to 0). |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1205 | */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 1206 | if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1207 | int want_clo = 1; |
| 1208 | /* If a close-spread-time option is set, we want to avoid |
| 1209 | * closing all the active HTTP connections at once so we add a |
| 1210 | * random factor that will spread the closing. |
| 1211 | */ |
| 1212 | if (tick_isset(global.close_spread_end)) { |
| 1213 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 1214 | if (remaining_window) { |
| 1215 | /* This should increase the closing rate the further along |
| 1216 | * the window we are. |
| 1217 | */ |
| 1218 | want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time)); |
| 1219 | } |
| 1220 | } |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 1221 | else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) |
| 1222 | want_clo = 0; |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1223 | |
| 1224 | if (want_clo) { |
| 1225 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 1226 | 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); |
| 1227 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1228 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /* Deduce the connection mode of the client connection, depending on the |
| 1232 | * configuration and the H1 message flags. This function is called twice, the |
| 1233 | * first time when the request is parsed and the second time when the response |
| 1234 | * is parsed. |
| 1235 | */ |
| 1236 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 1237 | { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1238 | struct session *sess = h1s->sess; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1239 | struct proxy *be = h1s->h1c->px; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1240 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1241 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1242 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1243 | /* Input direction: second pass */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1244 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1245 | h1s->status == 101) { |
| 1246 | /* Either we've established an explicit tunnel, or we're |
| 1247 | * switching the protocol. In both cases, we're very unlikely to |
| 1248 | * understand the next protocols. We have to switch to tunnel |
| 1249 | * mode, so that we transfer the request and responses then let |
| 1250 | * this protocol pass unmodified. When we later implement |
| 1251 | * specific parsers for such protocols, we'll want to check the |
| 1252 | * Upgrade header which contains information about that protocol |
| 1253 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 1254 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1255 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1256 | 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] | 1257 | } |
| 1258 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 1259 | /* By default the server is in KAL mode. CLOSE mode mean |
| 1260 | * it is imposed by haproxy itself. So only change KAL |
| 1261 | * mode here. */ |
| 1262 | if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO || |
| 1263 | !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){ |
| 1264 | /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */ |
| 1265 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1266 | 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] | 1267 | } |
| 1268 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1269 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1270 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1271 | /* Output direction: first pass */ |
| 1272 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 1273 | /* explicit close => close */ |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1274 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1275 | 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] | 1276 | } |
| 1277 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 1278 | ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 1279 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 1280 | (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 1281 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) { |
| 1282 | /* no explicit keep-alive option httpclose/server-close => close */ |
| 1283 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1284 | 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] | 1285 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1286 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 1289 | if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1290 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1291 | 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); |
| 1292 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1295 | 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] | 1296 | { |
| 1297 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1298 | |
| 1299 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1300 | * token is found |
| 1301 | */ |
| 1302 | 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] | 1303 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1304 | |
| 1305 | 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] | 1306 | if (!(h1m->flags & H1_MF_VER_11)) { |
| 1307 | 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] | 1308 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1309 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1310 | } |
| 1311 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1312 | if (h1m->flags & H1_MF_VER_11) { |
| 1313 | 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] | 1314 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1315 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1316 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1319 | 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] | 1320 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1321 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1322 | * token is found |
| 1323 | */ |
| 1324 | 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] | 1325 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1326 | |
| 1327 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1328 | if (!(h1m->flags & H1_MF_VER_11) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1329 | !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) { |
| 1330 | 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] | 1331 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1332 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1333 | } |
| 1334 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1335 | if (h1m->flags & H1_MF_VER_11) { |
| 1336 | 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] | 1337 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1338 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1339 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1340 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1341 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1342 | 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] | 1343 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1344 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1345 | h1_set_cli_conn_mode(h1s, h1m); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1346 | else |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1347 | h1_set_srv_conn_mode(h1s, h1m); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1350 | static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
| 1351 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1352 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1353 | h1_set_cli_conn_mode(h1s, h1m); |
| 1354 | else |
| 1355 | h1_set_srv_conn_mode(h1s, h1m); |
| 1356 | |
| 1357 | if (!(h1m->flags & H1_MF_RESP)) |
| 1358 | h1_update_req_conn_value(h1s, h1m, conn_val); |
| 1359 | else |
| 1360 | h1_update_res_conn_value(h1s, h1m, conn_val); |
| 1361 | } |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1362 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1363 | /* Try to adjust the case of the message header name using the global map |
| 1364 | * <hdrs_map>. |
| 1365 | */ |
| 1366 | static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name) |
| 1367 | { |
| 1368 | struct ebpt_node *node; |
| 1369 | struct h1_hdr_entry *entry; |
| 1370 | |
| 1371 | /* No entry in the map, do nothing */ |
| 1372 | if (eb_is_empty(&hdrs_map.map)) |
| 1373 | return; |
| 1374 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1375 | /* No conversion for the request headers */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1376 | if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV)) |
| 1377 | return; |
| 1378 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1379 | /* No conversion for the response headers */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1380 | if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI)) |
| 1381 | return; |
| 1382 | |
| 1383 | node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len); |
| 1384 | if (!node) |
| 1385 | return; |
| 1386 | entry = container_of(node, struct h1_hdr_entry, node); |
| 1387 | name->ptr = entry->name.ptr; |
| 1388 | name->len = entry->name.len; |
| 1389 | } |
| 1390 | |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1391 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 1392 | * The description must be small enough to always fit in a buffer. The output |
| 1393 | * buffer may be the trash so the trash must not be used inside this function. |
| 1394 | */ |
| 1395 | static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 1396 | { |
| 1397 | chunk_appendf(out, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1398 | " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n" |
| 1399 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 1400 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 1401 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 1402 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 1403 | 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] | 1404 | } |
| 1405 | /* |
| 1406 | * Capture a bad request or response and archive it in the proxy's structure. |
| 1407 | * By default it tries to report the error position as h1m->err_pos. However if |
| 1408 | * this one is not set, it will then report h1m->next, which is the last known |
| 1409 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 1410 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 1411 | * determined thanks to the h1m's flags. |
| 1412 | */ |
| 1413 | static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s, |
| 1414 | struct h1m *h1m, struct buffer *buf) |
| 1415 | { |
Christopher Faulet | db2c17d | 2020-10-15 17:19:46 +0200 | [diff] [blame] | 1416 | struct session *sess = h1s->sess; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1417 | struct proxy *proxy = h1c->px; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1418 | struct proxy *other_end; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1419 | union error_snapshot_ctx ctx; |
| 1420 | |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 1421 | if ((h1c->flags & H1C_F_ST_ATTACHED) && sc_strm(h1s_sc(h1s))) { |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1422 | if (sess == NULL) |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 1423 | sess = __sc_strm(h1s_sc(h1s))->sess; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1424 | if (!(h1m->flags & H1_MF_RESP)) |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 1425 | other_end = __sc_strm(h1s_sc(h1s))->be; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1426 | else |
| 1427 | other_end = sess->fe; |
| 1428 | } else |
| 1429 | other_end = NULL; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1430 | |
| 1431 | /* http-specific part now */ |
| 1432 | ctx.h1.state = h1m->state; |
| 1433 | ctx.h1.c_flags = h1c->flags; |
| 1434 | ctx.h1.s_flags = h1s->flags; |
| 1435 | ctx.h1.m_flags = h1m->flags; |
| 1436 | ctx.h1.m_clen = h1m->curr_len; |
| 1437 | ctx.h1.m_blen = h1m->body_len; |
| 1438 | |
| 1439 | proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end, |
| 1440 | h1c->conn->target, sess, buf, 0, 0, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1441 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 1442 | &ctx, h1_show_error_snapshot); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1445 | /* Emit the chunksize followed by a CRLF in front of data of the buffer |
| 1446 | * <buf>. It goes backwards and starts with the byte before the buffer's |
| 1447 | * head. The caller is responsible for ensuring there is enough room left before |
| 1448 | * the buffer's head for the string. |
| 1449 | */ |
| 1450 | static void h1_emit_chunk_size(struct buffer *buf, size_t chksz) |
| 1451 | { |
| 1452 | char *beg, *end; |
| 1453 | |
| 1454 | beg = end = b_head(buf); |
| 1455 | *--beg = '\n'; |
| 1456 | *--beg = '\r'; |
| 1457 | do { |
| 1458 | *--beg = hextab[chksz & 0xF]; |
| 1459 | } while (chksz >>= 4); |
| 1460 | buf->head -= (end - beg); |
| 1461 | b_add(buf, end - beg); |
| 1462 | } |
| 1463 | |
| 1464 | /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for |
| 1465 | * ensuring there is enough room left in the buffer for the string. */ |
| 1466 | static void h1_emit_chunk_crlf(struct buffer *buf) |
| 1467 | { |
| 1468 | *(b_peek(buf, b_data(buf))) = '\r'; |
| 1469 | *(b_peek(buf, b_data(buf) + 1)) = '\n'; |
| 1470 | b_add(buf, 2); |
| 1471 | } |
| 1472 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1473 | /* |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1474 | * Switch the stream to tunnel mode. This function must only be called on 2xx |
| 1475 | * (successful) replies to CONNECT requests or on 101 (switching protocol). |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1476 | */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1477 | static void h1_set_tunnel_mode(struct h1s *h1s) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1478 | { |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1479 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1480 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1481 | h1s->req.state = H1_MSG_TUNNEL; |
| 1482 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1483 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1484 | h1s->res.state = H1_MSG_TUNNEL; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1485 | h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1486 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1487 | TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 1488 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1489 | if (h1s->flags & H1S_F_RX_BLK) { |
| 1490 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1491 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1492 | TRACE_STATE("Re-enable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1493 | } |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1494 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1495 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1496 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1497 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1498 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1499 | } |
| 1500 | |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1501 | /* Search for a websocket key header. The message should have been identified |
| 1502 | * as a valid websocket handshake. |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1503 | * |
| 1504 | * On the request side, if found the key is stored in the session. It might be |
| 1505 | * needed to calculate response key if the server side is using http/2. |
| 1506 | * |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 1507 | * On the response side, the key might be verified if haproxy has been |
| 1508 | * responsible for the generation of a key. This happens when a h2 client is |
| 1509 | * interfaced with a h1 server. |
| 1510 | * |
| 1511 | * Returns 0 if no key found or invalid key |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1512 | */ |
| 1513 | static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx) |
| 1514 | { |
| 1515 | struct htx_blk *blk; |
| 1516 | enum htx_blk_type type; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1517 | struct ist n, v; |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1518 | int ws_key_found = 0, idx; |
| 1519 | |
| 1520 | idx = htx_get_head(htx); // returns the SL that we skip |
| 1521 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 1522 | blk = htx_get_blk(htx, idx); |
| 1523 | type = htx_get_blk_type(blk); |
| 1524 | |
| 1525 | if (type == HTX_BLK_UNUSED) |
| 1526 | continue; |
| 1527 | |
| 1528 | if (type != HTX_BLK_HDR) |
| 1529 | break; |
| 1530 | |
| 1531 | n = htx_get_blk_name(htx, blk); |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1532 | v = htx_get_blk_value(htx, blk); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1533 | |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1534 | /* Websocket key is base64 encoded of 16 bytes */ |
| 1535 | if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 && |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1536 | !(h1m->flags & H1_MF_RESP)) { |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1537 | /* Copy the key on request side |
| 1538 | * we might need it if the server is using h2 and does |
| 1539 | * not provide the response |
| 1540 | */ |
| 1541 | memcpy(h1s->ws_key, v.ptr, 24); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1542 | ws_key_found = 1; |
| 1543 | break; |
| 1544 | } |
| 1545 | else if (isteqi(n, ist("sec-websocket-accept")) && |
| 1546 | h1m->flags & H1_MF_RESP) { |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 1547 | /* Need to verify the response key if the input was |
| 1548 | * generated by haproxy |
| 1549 | */ |
| 1550 | if (h1s->ws_key[0]) { |
| 1551 | char key[29]; |
| 1552 | h1_calculate_ws_output_key(h1s->ws_key, key); |
| 1553 | if (!isteqi(ist(key), v)) |
| 1554 | break; |
| 1555 | } |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1556 | ws_key_found = 1; |
| 1557 | break; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | /* missing websocket key, reject the message */ |
| 1562 | if (!ws_key_found) { |
| 1563 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1570 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1571 | * 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] | 1572 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1573 | * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on |
| 1574 | * the function http_parse_msg_hdrs() to do the parsing. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1575 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1576 | static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1577 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1578 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1579 | union h1_sl h1sl; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1580 | int ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1581 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1582 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1583 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1584 | if (h1s->meth == HTTP_METH_CONNECT) |
| 1585 | h1m->flags |= H1_MF_METH_CONNECT; |
| 1586 | if (h1s->meth == HTTP_METH_HEAD) |
| 1587 | h1m->flags |= H1_MF_METH_HEAD; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1588 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1589 | ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1590 | if (ret <= 0) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1591 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1592 | if (ret == -1) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1593 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1594 | TRACE_ERROR("parsing error, reject H1 message", 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] | 1595 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1596 | } |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1597 | else if (ret == -2) { |
| 1598 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1599 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1600 | } |
| 1601 | ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1602 | goto end; |
| 1603 | } |
| 1604 | |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1605 | |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 1606 | /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if |
| 1607 | * accept_payload_with_any_method global option is set. |
| 1608 | *There is a payload if the c-l is not null or the the payload is |
| 1609 | * chunk-encoded. A parsing error is reported but a A |
| 1610 | * 413-Payload-Too-Large is returned instead of a 400-Bad-Request. |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1611 | */ |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 1612 | if (!accept_payload_with_any_method && |
| 1613 | !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) && |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1614 | (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) && |
| 1615 | (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) { |
| 1616 | h1s->flags |= H1S_F_PARSING_ERROR; |
| 1617 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1618 | h1s->h1c->errcode = 413; |
| 1619 | TRACE_ERROR("HTTP/1.0 GET/HEAD/DELETE request with a payload forbidden", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
| 1620 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1621 | ret = 0; |
| 1622 | goto end; |
| 1623 | } |
| 1624 | |
Christopher Faulet | da3adeb | 2021-09-28 09:50:07 +0200 | [diff] [blame] | 1625 | /* Reject any message with an unknown transfer-encoding. In fact if any |
| 1626 | * encoding other than "chunked". A 422-Unprocessable-Content is |
| 1627 | * returned for an invalid request, a 502-Bad-Gateway for an invalid |
| 1628 | * response. |
| 1629 | */ |
| 1630 | if (h1m->flags & H1_MF_TE_OTHER) { |
| 1631 | h1s->flags |= H1S_F_PARSING_ERROR; |
| 1632 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1633 | if (!(h1m->flags & H1_MF_RESP)) |
| 1634 | h1s->h1c->errcode = 422; |
| 1635 | TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
| 1636 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1637 | ret = 0; |
| 1638 | goto end; |
| 1639 | } |
| 1640 | |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1641 | /* If websocket handshake, search for the websocket key */ |
| 1642 | if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == |
| 1643 | (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) { |
| 1644 | int ws_ret = h1_search_websocket_key(h1s, h1m, htx); |
| 1645 | if (!ws_ret) { |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1646 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1647 | TRACE_ERROR("missing/invalid websocket key, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1648 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1649 | |
| 1650 | ret = 0; |
| 1651 | goto end; |
| 1652 | } |
| 1653 | } |
| 1654 | |
Christopher Faulet | 486498c | 2019-10-11 14:22:00 +0200 | [diff] [blame] | 1655 | if (h1m->err_pos >= 0) { |
| 1656 | /* Maybe we found an error during the parsing while we were |
| 1657 | * configured not to block on that, so we have to capture it |
| 1658 | * now. |
| 1659 | */ |
| 1660 | TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
| 1661 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1662 | } |
| 1663 | |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1664 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1665 | h1s->meth = h1sl.rq.meth; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1666 | if (h1s->meth == HTTP_METH_HEAD) |
| 1667 | h1s->flags |= H1S_F_BODYLESS_RESP; |
| 1668 | } |
| 1669 | else { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1670 | h1s->status = h1sl.st.status; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1671 | if (h1s->status == 204 || h1s->status == 304) |
| 1672 | h1s->flags |= H1S_F_BODYLESS_RESP; |
| 1673 | } |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1674 | h1_process_input_conn_mode(h1s, h1m, htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1675 | *ofs += ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1676 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1677 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1678 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1679 | return ret; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1683 | * 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] | 1684 | * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag. |
| 1685 | * 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] | 1686 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1687 | static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx, |
| 1688 | struct buffer *buf, size_t *ofs, size_t max, |
| 1689 | struct buffer *htxbuf) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1690 | { |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 1691 | size_t ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1692 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1693 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1694 | ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1695 | if (!ret) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1696 | 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] | 1697 | if ((*htx)->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1698 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1699 | TRACE_ERROR("parsing error, reject H1 message", 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] | 1700 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1701 | } |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1702 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1705 | *ofs += ret; |
| 1706 | |
| 1707 | end: |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1708 | if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) { |
| 1709 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1710 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1711 | } |
| 1712 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1713 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1714 | return ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1718 | * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 1719 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1720 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1721 | * responsible to update the parser state <h1m>. If more room is requested, |
| 1722 | * H1S_F_RX_CONGESTED flag is set. |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1723 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1724 | static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1725 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1726 | { |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1727 | int ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1728 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1729 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1730 | ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1731 | if (ret <= 0) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1732 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1733 | if (ret == -1) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1734 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1735 | TRACE_ERROR("parsing error, reject H1 message", 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] | 1736 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1737 | } |
Christopher Faulet | ae660be | 2022-04-13 17:48:54 +0200 | [diff] [blame] | 1738 | else if (ret == -2) { |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1739 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1740 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1741 | } |
| 1742 | ret = 0; |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1743 | goto end; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1744 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1745 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1746 | *ofs += ret; |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1747 | |
| 1748 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1749 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1750 | return ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1754 | * 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] | 1755 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1756 | * it couldn't proceed. |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1757 | * |
| 1758 | * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1759 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1760 | static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1761 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1762 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1763 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1764 | struct htx *htx; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1765 | size_t data; |
| 1766 | size_t ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1767 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1768 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1769 | htx = htx_from_buf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1770 | 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] | 1771 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1772 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 7402776 | 2019-02-26 14:45:05 +0100 | [diff] [blame] | 1773 | data = htx->data; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1774 | |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 1775 | if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) |
Christopher Faulet | 0e54d54 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1776 | goto end; |
| 1777 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1778 | if (h1s->flags & H1S_F_RX_BLK) |
Christopher Faulet | ec4207c | 2021-04-08 18:34:30 +0200 | [diff] [blame] | 1779 | goto out; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1780 | |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1781 | /* Always remove congestion flags and try to process more input data */ |
| 1782 | h1s->flags &= ~H1S_F_RX_CONGESTED; |
| 1783 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1784 | do { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1785 | size_t used = htx_used_space(htx); |
| 1786 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1787 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1788 | TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1789 | ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1790 | if (!ret) |
| 1791 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1792 | |
| 1793 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"), |
| 1794 | H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret}); |
| 1795 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1796 | if ((h1m->flags & H1_MF_RESP) && |
| 1797 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1798 | h1m_init_res(&h1s->res); |
| 1799 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1800 | 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] | 1801 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1802 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1803 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1804 | TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1805 | ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 16a524c | 2021-02-02 21:16:03 +0100 | [diff] [blame] | 1806 | if (h1m->state < H1_MSG_TRAILERS) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1807 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1808 | |
| 1809 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"), |
| 1810 | H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret}); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1811 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1812 | else if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1813 | TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1814 | ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 16a524c | 2021-02-02 21:16:03 +0100 | [diff] [blame] | 1815 | if (h1m->state != H1_MSG_DONE) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1816 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1817 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1818 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"), |
| 1819 | 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] | 1820 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1821 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1822 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"), |
| 1823 | H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1824 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1825 | if ((h1m->flags & H1_MF_RESP) && |
| 1826 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) |
| 1827 | h1_set_tunnel_mode(h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1828 | else { |
| 1829 | if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) { |
| 1830 | /* Unfinished transaction: block this input side waiting the end of the output side */ |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1831 | h1s->flags |= H1S_F_RX_BLK; |
| 1832 | TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1833 | } |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1834 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1835 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1836 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1837 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1838 | } |
Christopher Faulet | 23021ad | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1839 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1840 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1841 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1842 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1843 | TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1844 | ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1845 | if (!ret) |
| 1846 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1847 | |
| 1848 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"), |
| 1849 | 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] | 1850 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1851 | else { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1852 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1853 | break; |
| 1854 | } |
| 1855 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1856 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1857 | } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR|H1S_F_RX_BLK|H1S_F_RX_CONGESTED))); |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1858 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1859 | |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 1860 | if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1861 | TRACE_ERROR("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1862 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1863 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1864 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1865 | b_del(&h1c->ibuf, total); |
| 1866 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1867 | htx_to_buf(htx, buf); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1868 | TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret}); |
| 1869 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1870 | ret = htx->data - data; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1871 | 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] | 1872 | h1c->flags &= ~H1C_F_IN_FULL; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1873 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 1874 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1875 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1876 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1877 | if (!b_data(&h1c->ibuf)) |
| 1878 | h1_release_buf(h1c, &h1c->ibuf); |
| 1879 | |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 1880 | if (!(h1c->flags & H1C_F_ST_READY)) { |
| 1881 | /* The H1 connection is not ready. Most of time, there is no CS |
| 1882 | * attached, except for TCP>H1 upgrade, from a TCP frontend. In both |
| 1883 | * cases, it is only possible on the client side. |
| 1884 | */ |
| 1885 | BUG_ON(h1c->flags & H1C_F_IS_BACK); |
| 1886 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1887 | if (h1m->state <= H1_MSG_LAST_LF) { |
| 1888 | TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
| 1889 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 1890 | goto end; |
| 1891 | } |
| 1892 | |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1893 | if (!(h1c->flags & H1C_F_ST_ATTACHED)) { |
| 1894 | TRACE_DEVEL("request headers fully parsed, create and attach the CS", H1_EV_RX_DATA, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1895 | BUG_ON(h1s_sc(h1s)); |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 1896 | if (!h1s_new_sc(h1s, buf)) { |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1897 | h1c->flags |= H1C_F_ST_ERROR; |
| 1898 | goto err; |
| 1899 | } |
| 1900 | } |
| 1901 | else { |
| 1902 | TRACE_DEVEL("request headers fully parsed, upgrade the inherited CS", H1_EV_RX_DATA, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1903 | BUG_ON(h1s_sc(h1s) == NULL); |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 1904 | if (!h1s_upgrade_sc(h1s, buf)) { |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1905 | h1c->flags |= H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1906 | TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1907 | goto err; |
| 1908 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1912 | /* Here h1s_sc(h1s) is always defined */ |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 1913 | if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) { |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1914 | TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1915 | se_fl_set(h1s->endp, SE_FL_MAY_SPLICE); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1916 | } |
| 1917 | else { |
| 1918 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1919 | se_fl_clr(h1s->endp, SE_FL_MAY_SPLICE); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1920 | } |
| 1921 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1922 | /* Set EOI on stream connector in DONE state iff: |
Christopher Faulet | a22782b | 2021-02-08 17:18:01 +0100 | [diff] [blame] | 1923 | * - it is a response |
| 1924 | * - it is a request but no a protocol upgrade nor a CONNECT |
| 1925 | * |
| 1926 | * If not set, Wait the response to do so or not depending on the status |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1927 | * code. |
Christopher Faulet | 3e1748b | 2020-12-07 18:21:27 +0100 | [diff] [blame] | 1928 | */ |
Christopher Faulet | a22782b | 2021-02-08 17:18:01 +0100 | [diff] [blame] | 1929 | if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) || |
| 1930 | ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1931 | se_fl_set(h1s->endp, SE_FL_EOI); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1932 | |
Christopher Faulet | ec4207c | 2021-04-08 18:34:30 +0200 | [diff] [blame] | 1933 | out: |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1934 | /* When Input data are pending for this message, notify upper layer that |
| 1935 | * the mux need more space in the HTX buffer to continue if : |
| 1936 | * |
| 1937 | * - The parser is blocked in MSG_DATA or MSG_TUNNEL state |
| 1938 | * - Headers or trailers are pending to be copied. |
| 1939 | */ |
| 1940 | if (h1s->flags & (H1S_F_RX_CONGESTED)) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1941 | se_fl_set(h1s->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1942 | TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
| 1943 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1944 | else { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1945 | se_fl_clr(h1s->endp, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1946 | if (h1s->flags & H1S_F_REOS) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1947 | se_fl_set(h1s->endp, SE_FL_EOS); |
Christopher Faulet | 1e85778 | 2020-12-08 10:38:22 +0100 | [diff] [blame] | 1948 | if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) { |
| 1949 | /* DONE or TUNNEL or SHUTR without XFER_LEN, set |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1950 | * EOI on the stream connector */ |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1951 | se_fl_set(h1s->endp, SE_FL_EOI); |
Christopher Faulet | 3e1748b | 2020-12-07 18:21:27 +0100 | [diff] [blame] | 1952 | } |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1953 | else if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1954 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1955 | TRACE_ERROR("message aborted, set error on CS", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 1956 | } |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1957 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1958 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1959 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1960 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1961 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1962 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1963 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1964 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1965 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1966 | end: |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1967 | 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] | 1968 | return ret; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1969 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1970 | err: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1971 | htx_to_buf(htx, buf); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 1972 | se_fl_set(h1s->endp, SE_FL_EOI); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1973 | 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] | 1974 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1977 | /* |
| 1978 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1979 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1980 | * 0 if it couldn't proceed. |
| 1981 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1982 | static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1983 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1984 | struct h1s *h1s = h1c->h1s; |
| 1985 | struct h1m *h1m; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1986 | struct htx *chn_htx = NULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1987 | struct htx_blk *blk; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1988 | struct buffer tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1989 | size_t total = 0; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1990 | int last_data = 0; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1991 | int ws_key_found = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1992 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1993 | chn_htx = htxbuf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1994 | TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
| 1995 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1996 | if (htx_is_empty(chn_htx)) |
| 1997 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1998 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1999 | if (h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2000 | goto end; |
| 2001 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2002 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 2003 | h1c->flags |= H1C_F_OUT_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2004 | 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] | 2005 | goto end; |
| 2006 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2007 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 2008 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2009 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 2010 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2011 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2012 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2013 | /* Perform some optimizations to reduce the number of buffer copies. |
| 2014 | * First, if the mux's buffer is empty and the htx area contains |
| 2015 | * exactly one data block of the same size as the requested count, |
| 2016 | * then it's possible to simply swap the caller's buffer with the |
| 2017 | * mux's output buffer and adjust offsets and length to match the |
| 2018 | * entire DATA HTX block in the middle. In this case we perform a |
| 2019 | * true zero-copy operation from end-to-end. This is the situation |
| 2020 | * that happens all the time with large files. Second, if this is not |
| 2021 | * possible, but the mux's output buffer is empty, we still have an |
| 2022 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 2023 | * the intermediary buffer's area point to the output buffer's area. |
| 2024 | * In this case we want to skip the HTX header to make sure that copies |
| 2025 | * remain aligned and that this operation remains possible all the |
| 2026 | * time. This goes for headers, data blocks and any data extracted from |
| 2027 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2028 | */ |
| 2029 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2030 | if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) && |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2031 | (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) && |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2032 | htx_nbblks(chn_htx) == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 2033 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2034 | htx_get_blk_value(chn_htx, blk).len == count) { |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2035 | void *old_area; |
| 2036 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2037 | TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2038 | if (h1m->state == H1_MSG_DATA) { |
| 2039 | if (h1m->flags & H1_MF_CLEN) { |
| 2040 | if (count > h1m->curr_len) { |
| 2041 | TRACE_ERROR("too much payload, more than announced", |
| 2042 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2043 | goto error; |
| 2044 | } |
| 2045 | h1m->curr_len -= count; |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2046 | if (!h1m->curr_len) |
| 2047 | last_data = 1; |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2048 | } |
| 2049 | if (chn_htx->flags & HTX_FL_EOM) { |
| 2050 | TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
| 2051 | last_data = 1; |
| 2052 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2055 | old_area = h1c->obuf.area; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2056 | h1c->obuf.area = buf->area; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2057 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2058 | h1c->obuf.data = count; |
| 2059 | |
| 2060 | buf->area = old_area; |
| 2061 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2062 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2063 | chn_htx = (struct htx *)buf->area; |
| 2064 | htx_reset(chn_htx); |
| 2065 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2066 | /* The message is chunked. We need to emit the chunk |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2067 | * size and eventually the last chunk. We have at least |
| 2068 | * the size of the struct htx to write the chunk |
| 2069 | * envelope. It should be enough. |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2070 | */ |
| 2071 | if (h1m->flags & H1_MF_CHNK) { |
| 2072 | h1_emit_chunk_size(&h1c->obuf, count); |
| 2073 | h1_emit_chunk_crlf(&h1c->obuf); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2074 | if (last_data) { |
| 2075 | /* Emit the last chunk too at the buffer's end */ |
| 2076 | b_putblk(&h1c->obuf, "0\r\n\r\n", 5); |
| 2077 | } |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2080 | if (h1m->state == H1_MSG_DATA) |
| 2081 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2082 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2083 | else |
| 2084 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2085 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2086 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2087 | total += count; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2088 | if (last_data) { |
| 2089 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2090 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2091 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2092 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2093 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"), |
| 2097 | H1_EV_TX_DATA, h1c->conn, h1s); |
| 2098 | } |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2099 | goto out; |
| 2100 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2101 | tmp.area = h1c->obuf.area + h1c->obuf.head; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2102 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2103 | else |
| 2104 | tmp.area = trash.area; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2105 | |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2106 | tmp.data = 0; |
| 2107 | tmp.size = b_room(&h1c->obuf); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2108 | while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2109 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2110 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2111 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2112 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2113 | uint32_t vlen, chklen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2114 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2115 | vlen = sz; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2116 | if (type != HTX_BLK_DATA && vlen > count) |
| 2117 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2118 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2119 | if (type == HTX_BLK_UNUSED) |
| 2120 | goto nextblk; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2121 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2122 | switch (h1m->state) { |
| 2123 | case H1_MSG_RQBEFORE: |
| 2124 | if (type != HTX_BLK_REQ_SL) |
| 2125 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2126 | 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] | 2127 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2128 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2129 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2130 | if (!h1_format_htx_reqline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2131 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2132 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 2133 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 2134 | h1m->flags |= H1_MF_CLEN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2135 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 2136 | if (h1s->meth == HTTP_METH_HEAD) |
| 2137 | h1s->flags |= H1S_F_BODYLESS_RESP; |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2138 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2139 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2140 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2141 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 2142 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2143 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2144 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2145 | case H1_MSG_RPBEFORE: |
| 2146 | if (type != HTX_BLK_RES_SL) |
| 2147 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2148 | 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] | 2149 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2150 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2151 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2152 | if (!h1_format_htx_stline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2153 | goto full; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 2154 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2155 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | f3e7619 | 2020-12-02 16:46:33 +0100 | [diff] [blame] | 2156 | if (h1s->status < 200) |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2157 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 2158 | else if (h1s->status == 204 || h1s->status == 304) |
| 2159 | h1s->flags |= H1S_F_BODYLESS_RESP; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2160 | h1m->state = H1_MSG_HDR_FIRST; |
| 2161 | break; |
| 2162 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2163 | case H1_MSG_HDR_FIRST: |
| 2164 | case H1_MSG_HDR_NAME: |
| 2165 | case H1_MSG_HDR_L2_LWS: |
| 2166 | if (type == HTX_BLK_EOH) |
| 2167 | goto last_lf; |
| 2168 | if (type != HTX_BLK_HDR) |
| 2169 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2170 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2171 | h1m->state = H1_MSG_HDR_NAME; |
| 2172 | n = htx_get_blk_name(chn_htx, blk); |
| 2173 | v = htx_get_blk_value(chn_htx, blk); |
| 2174 | |
Christopher Faulet | 86d144c | 2019-08-14 16:32:25 +0200 | [diff] [blame] | 2175 | /* Skip all pseudo-headers */ |
| 2176 | if (*(n.ptr) == ':') |
| 2177 | goto skip_hdr; |
| 2178 | |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2179 | if (isteq(n, ist("transfer-encoding"))) { |
| 2180 | if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204)) |
| 2181 | goto skip_hdr; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2182 | h1_parse_xfer_enc_header(h1m, v); |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2183 | } |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 2184 | else if (isteq(n, ist("content-length"))) { |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2185 | if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204)) |
| 2186 | goto skip_hdr; |
Christopher Faulet | 5220ef2 | 2019-03-27 15:44:56 +0100 | [diff] [blame] | 2187 | /* Only skip C-L header with invalid value. */ |
| 2188 | if (h1_parse_cont_len_header(h1m, &v) < 0) |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 2189 | goto skip_hdr; |
| 2190 | } |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 2191 | else if (isteq(n, ist("connection"))) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 2192 | h1_parse_connection_header(h1m, &v); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2193 | if (!v.len) |
| 2194 | goto skip_hdr; |
| 2195 | } |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2196 | else if (isteq(n, ist("upgrade"))) { |
| 2197 | h1_parse_upgrade_header(h1m, v); |
| 2198 | } |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 2199 | else if ((isteq(n, ist("sec-websocket-accept")) && |
| 2200 | h1m->flags & H1_MF_RESP) || |
| 2201 | (isteq(n, ist("sec-websocket-key")) && |
| 2202 | !(h1m->flags & H1_MF_RESP))) { |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2203 | ws_key_found = 1; |
| 2204 | } |
Christopher Faulet | f56e846 | 2021-09-28 10:56:36 +0200 | [diff] [blame] | 2205 | else if (isteq(n, ist("te"))) { |
| 2206 | /* "te" may only be sent with "trailers" if this value |
| 2207 | * is present, otherwise it must be deleted. |
| 2208 | */ |
| 2209 | v = istist(v, ist("trailers")); |
| 2210 | if (!isttest(v) || (v.len > 8 && v.ptr[8] != ',')) |
| 2211 | goto skip_hdr; |
| 2212 | v = ist("trailers"); |
| 2213 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2214 | |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 2215 | /* Skip header if same name is used to add the server name */ |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2216 | if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) && |
| 2217 | isteqi(n, h1c->px->server_id_hdr_name)) |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 2218 | goto skip_hdr; |
| 2219 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2220 | /* Try to adjust the case of the header name */ |
| 2221 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2222 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2223 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2224 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2225 | skip_hdr: |
| 2226 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 2227 | break; |
| 2228 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2229 | case H1_MSG_LAST_LF: |
| 2230 | if (type != HTX_BLK_EOH) |
| 2231 | goto error; |
| 2232 | last_lf: |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2233 | h1m->state = H1_MSG_LAST_LF; |
| 2234 | if (!(h1s->flags & H1S_F_HAVE_O_CONN)) { |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2235 | if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) { |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 2236 | /* If the reply comes from haproxy while the request is |
| 2237 | * not finished, we force the connection close. */ |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2238 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 2239 | TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2240 | } |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 2241 | else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) { |
| 2242 | /* T-E + C-L: force close */ |
| 2243 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 2244 | TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2245 | } |
| 2246 | else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) { |
| 2247 | /* T-E + HTTP/1.0: force close */ |
| 2248 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 2249 | TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2250 | } |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2251 | |
| 2252 | /* the conn_mode must be processed. So do it */ |
Christopher Faulet | a110ecb | 2019-06-17 14:07:46 +0200 | [diff] [blame] | 2253 | n = ist("connection"); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2254 | v = ist(""); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 2255 | h1_process_output_conn_mode(h1s, h1m, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2256 | if (v.len) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2257 | /* Try to adjust the case of the header name */ |
| 2258 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2259 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2260 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2261 | goto full; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2262 | } |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2263 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2264 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 2265 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2266 | if ((h1s->meth != HTTP_METH_CONNECT && |
| 2267 | (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] | 2268 | (H1_MF_VER_11|H1_MF_XFER_LEN)) || |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2269 | (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) && |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2270 | !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) && |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 2271 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
| 2272 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 2273 | /* chunking needed but header not seen */ |
Christopher Faulet | 7a991a9 | 2019-10-04 10:23:51 +0200 | [diff] [blame] | 2274 | n = ist("transfer-encoding"); |
| 2275 | v = ist("chunked"); |
| 2276 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2277 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2278 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2279 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2280 | 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] | 2281 | h1m->flags |= H1_MF_CHNK; |
| 2282 | } |
| 2283 | |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2284 | /* Now add the server name to a header (if requested) */ |
| 2285 | if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) && |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2286 | !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) { |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2287 | struct server *srv = objt_server(h1c->conn->target); |
| 2288 | |
| 2289 | if (srv) { |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2290 | n = h1c->px->server_id_hdr_name; |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2291 | v = ist(srv->id); |
Christopher Faulet | 5cef2a6 | 2019-10-02 11:06:13 +0200 | [diff] [blame] | 2292 | |
| 2293 | /* Try to adjust the case of the header name */ |
| 2294 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2295 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2296 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2297 | goto full; |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2298 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2299 | 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] | 2300 | h1s->flags |= H1S_F_HAVE_SRV_NAME; |
| 2301 | } |
| 2302 | |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2303 | /* Add websocket handshake key if needed */ |
| 2304 | if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET) && |
| 2305 | !ws_key_found) { |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 2306 | if (!(h1m->flags & H1_MF_RESP)) { |
| 2307 | /* generate a random websocket key |
| 2308 | * stored in the session to |
| 2309 | * verify it on the response side |
| 2310 | */ |
| 2311 | h1_generate_random_ws_input_key(h1s->ws_key); |
| 2312 | |
| 2313 | if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"), |
| 2314 | ist(h1s->ws_key), |
| 2315 | &tmp)) { |
| 2316 | goto full; |
| 2317 | } |
| 2318 | } |
| 2319 | else { |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2320 | /* add the response header key */ |
| 2321 | char key[29]; |
| 2322 | h1_calculate_ws_output_key(h1s->ws_key, key); |
| 2323 | if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"), |
| 2324 | ist(key), |
| 2325 | &tmp)) { |
| 2326 | goto full; |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2331 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"), |
| 2332 | H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
| 2333 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2334 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2335 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2336 | goto full; |
| 2337 | goto done; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2338 | } |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2339 | else if ((h1m->flags & H1_MF_RESP) && |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2340 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2341 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2342 | goto full; |
| 2343 | goto done; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2344 | } |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 2345 | else if ((h1m->flags & H1_MF_RESP) && |
| 2346 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2347 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2348 | goto full; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 2349 | h1m_init_res(&h1s->res); |
| 2350 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2351 | h1s->flags &= ~H1S_F_HAVE_O_CONN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2352 | 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] | 2353 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2354 | else { |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2355 | /* EOM flag is set or empty payload (C-L to 0) and it is the last block */ |
| 2356 | if (htx_is_unique_blk(chn_htx, blk) && |
| 2357 | ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) { |
Christopher Faulet | 3d6e0e3 | 2021-02-08 09:34:35 +0100 | [diff] [blame] | 2358 | if (h1m->flags & H1_MF_CHNK) { |
| 2359 | if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7)) |
| 2360 | goto full; |
| 2361 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2362 | else if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2363 | goto full; |
| 2364 | goto done; |
| 2365 | } |
| 2366 | else if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2367 | goto full; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2368 | h1m->state = H1_MSG_DATA; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2369 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2370 | break; |
| 2371 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2372 | case H1_MSG_DATA: |
Christopher Faulet | f8db73e | 2019-07-04 17:12:12 +0200 | [diff] [blame] | 2373 | case H1_MSG_TUNNEL: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2374 | if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) { |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2375 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) |
| 2376 | goto trailers; |
| 2377 | |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 2378 | /* If the message is not chunked, never |
| 2379 | * add the last chunk. */ |
| 2380 | 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] | 2381 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2382 | 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] | 2383 | goto trailers; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2384 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2385 | else if (type != HTX_BLK_DATA) |
| 2386 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2387 | |
| 2388 | 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] | 2389 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2390 | /* It is the last block of this message. After this one, |
| 2391 | * only tunneled data may be forwarded. */ |
| 2392 | if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) { |
| 2393 | TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
| 2394 | last_data = 1; |
| 2395 | } |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2396 | |
| 2397 | if (vlen > count) { |
| 2398 | /* Get the maximum amount of data we can xferred */ |
| 2399 | vlen = count; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2400 | last_data = 0; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2403 | if (h1m->state == H1_MSG_DATA) { |
| 2404 | if (h1m->flags & H1_MF_CLEN) { |
| 2405 | if (vlen > h1m->curr_len) { |
| 2406 | TRACE_ERROR("too much payload, more than announced", |
| 2407 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2408 | goto error; |
| 2409 | } |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2410 | } |
| 2411 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) { |
| 2412 | TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx); |
| 2413 | goto skip_data; |
| 2414 | } |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2415 | } |
| 2416 | |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2417 | chklen = 0; |
| 2418 | if (h1m->flags & H1_MF_CHNK) { |
| 2419 | chklen = b_room(&tmp); |
| 2420 | chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 2421 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 2422 | (chklen < 1048576) ? 5 : 8); |
| 2423 | chklen += 4; /* 2 x CRLF */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2424 | |
| 2425 | /* If it is the end of the chunked message (without EOT), reserve the |
| 2426 | * last chunk size */ |
| 2427 | if (last_data) |
| 2428 | chklen += 5; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | if (vlen + chklen > b_room(&tmp)) { |
| 2432 | /* too large for the buffer */ |
| 2433 | if (chklen >= b_room(&tmp)) |
| 2434 | goto full; |
| 2435 | vlen = b_room(&tmp) - chklen; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2436 | last_data = 0; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2437 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2438 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2439 | v.len = vlen; |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2440 | if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2441 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2442 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2443 | /* Space already reserved, so it must succeed */ |
| 2444 | if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5)) |
| 2445 | goto error; |
| 2446 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2447 | if (h1m->state == H1_MSG_DATA) |
| 2448 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2449 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2450 | else |
| 2451 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2452 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len}); |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2453 | |
| 2454 | skip_data: |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2455 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | b4eca0e | 2022-01-10 17:27:51 +0100 | [diff] [blame] | 2456 | h1m->curr_len -= vlen; |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2457 | if (!h1m->curr_len) |
| 2458 | last_data = 1; |
| 2459 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2460 | if (last_data) |
| 2461 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2462 | break; |
| 2463 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2464 | case H1_MSG_TRAILERS: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2465 | if (type != HTX_BLK_TLR && type != HTX_BLK_EOT) |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2466 | goto error; |
| 2467 | trailers: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2468 | h1m->state = H1_MSG_TRAILERS; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2469 | |
Christopher Faulet | d934e8d | 2022-05-05 09:39:42 +0200 | [diff] [blame] | 2470 | if (!(h1m->flags & H1_MF_CHNK)) |
| 2471 | goto done; |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 2472 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2473 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) { |
| 2474 | TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx); |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2475 | if (type == HTX_BLK_EOT) |
| 2476 | goto done; |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2477 | break; |
| 2478 | } |
| 2479 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2480 | if (type == HTX_BLK_EOT) { |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2481 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2482 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2483 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"), |
| 2484 | H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2485 | goto done; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 2486 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2487 | else { // HTX_BLK_TLR |
| 2488 | n = htx_get_blk_name(chn_htx, blk); |
| 2489 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2490 | |
| 2491 | /* Try to adjust the case of the header name */ |
| 2492 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2493 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2494 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2495 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2496 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2497 | break; |
| 2498 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2499 | case H1_MSG_DONE: |
Christopher Faulet | d934e8d | 2022-05-05 09:39:42 +0200 | [diff] [blame] | 2500 | /* If the message is not chunked, ignore |
| 2501 | * trailers. It may happen with H2 messages. */ |
| 2502 | if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK)) |
| 2503 | break; |
| 2504 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2505 | TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2506 | goto error; /* For now return an error */ |
| 2507 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2508 | done: |
| 2509 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2510 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2511 | h1s->flags |= H1S_F_TX_BLK; |
| 2512 | TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2513 | } |
| 2514 | else if ((h1m->flags & H1_MF_RESP) && |
| 2515 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) { |
| 2516 | /* a successful reply to a CONNECT or a protocol switching is sent |
| 2517 | * to the client. Switch the response to tunnel mode. |
| 2518 | */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 2519 | h1_set_tunnel_mode(h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2520 | TRACE_STATE("switch H1 response in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2521 | } |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 2522 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2523 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2524 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2525 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2526 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | cd67bff | 2019-06-14 16:54:15 +0200 | [diff] [blame] | 2527 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2528 | |
| 2529 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"), |
| 2530 | H1_EV_TX_DATA, h1c->conn, h1s); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2531 | break; |
| 2532 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2533 | default: |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2534 | error: |
Christopher Faulet | d87d3fa | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 2535 | /* Unexpected error during output processing */ |
Christopher Faulet | 69b4821 | 2019-09-09 10:11:30 +0200 | [diff] [blame] | 2536 | chn_htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 2537 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2538 | h1c->flags |= H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 2539 | TRACE_ERROR("processing output error, set error on h1c/h1s", |
| 2540 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2541 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2542 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2543 | |
| 2544 | nextblk: |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2545 | total += vlen; |
| 2546 | count -= vlen; |
| 2547 | if (sz == vlen) |
| 2548 | blk = htx_remove_blk(chn_htx, blk); |
| 2549 | else { |
| 2550 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 2551 | break; |
| 2552 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2555 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2556 | /* when the output buffer is empty, tmp shares the same area so that we |
| 2557 | * only have to update pointers and lengths. |
| 2558 | */ |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2559 | if (tmp.area == h1c->obuf.area + h1c->obuf.head) |
| 2560 | h1c->obuf.data = tmp.data; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2561 | else |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2562 | b_putblk(&h1c->obuf, tmp.area, tmp.data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2563 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2564 | htx_to_buf(chn_htx, buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2565 | out: |
| 2566 | if (!buf_room_for_htx_data(&h1c->obuf)) { |
| 2567 | 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] | 2568 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2569 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2570 | end: |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2571 | /* Both the request and the response reached the DONE state. So set EOI |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2572 | * flag on the stream connector. Most of time, the flag will already be set, |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2573 | * except for protocol upgrades. Report an error if data remains blocked |
| 2574 | * in the output buffer. |
| 2575 | */ |
| 2576 | if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { |
| 2577 | if (!htx_is_empty(chn_htx)) { |
| 2578 | h1c->flags |= H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 2579 | TRACE_ERROR("txn done but data waiting to be sent, set error on h1c", H1_EV_H1C_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2580 | } |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 2581 | se_fl_set(h1s->endp, SE_FL_EOI); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2584 | 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] | 2585 | return total; |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2586 | |
| 2587 | full: |
| 2588 | TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
| 2589 | h1c->flags |= H1C_F_OUT_FULL; |
| 2590 | goto copy; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2591 | } |
| 2592 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2593 | /*********************************************************/ |
| 2594 | /* functions below are I/O callbacks from the connection */ |
| 2595 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2596 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 2597 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2598 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2599 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2600 | tasklet_wakeup(h1s->subs->tasklet); |
| 2601 | h1s->subs->events &= ~SUB_RETRY_RECV; |
| 2602 | if (!h1s->subs->events) |
| 2603 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 2607 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2608 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2609 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2610 | tasklet_wakeup(h1s->subs->tasklet); |
| 2611 | h1s->subs->events &= ~SUB_RETRY_SEND; |
| 2612 | if (!h1s->subs->events) |
| 2613 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2614 | } |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2615 | } |
| 2616 | |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2617 | /* alerts the data layer following this sequence : |
| 2618 | * - if the h1s' data layer is subscribed to recv, then it's woken up for recv |
| 2619 | * - if its subscribed to send, then it's woken up for send |
| 2620 | * - if it was subscribed to neither, its ->wake() callback is called |
| 2621 | */ |
| 2622 | static void h1_alert(struct h1s *h1s) |
| 2623 | { |
| 2624 | if (h1s->subs) { |
| 2625 | h1_wake_stream_for_recv(h1s); |
| 2626 | h1_wake_stream_for_send(h1s); |
| 2627 | } |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 2628 | else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) { |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2629 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 2630 | h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s)); |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2634 | /* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success |
| 2635 | * and 0 on error. The flag H1C_F_ERR_PENDING is set on the H1 connection for |
| 2636 | * retryable errors (allocation error or buffer full). On success, the error is |
| 2637 | * copied in the output buffer. |
| 2638 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2639 | static int h1_send_error(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2640 | { |
| 2641 | int rc = http_get_status_idx(h1c->errcode); |
| 2642 | int ret = 0; |
| 2643 | |
| 2644 | TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode}); |
| 2645 | |
| 2646 | /* Verify if the error is mapped on /dev/null or any empty file */ |
| 2647 | /// XXX: do a function ! |
| 2648 | if (h1c->px->replies[rc] && |
| 2649 | h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG && |
| 2650 | h1c->px->replies[rc]->body.errmsg && |
| 2651 | b_is_null(h1c->px->replies[rc]->body.errmsg)) { |
| 2652 | /* Empty error, so claim a success */ |
| 2653 | ret = 1; |
| 2654 | goto out; |
| 2655 | } |
| 2656 | |
| 2657 | if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) { |
| 2658 | h1c->flags |= H1C_F_ERR_PENDING; |
| 2659 | goto out; |
| 2660 | } |
| 2661 | |
| 2662 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 2663 | h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ERR_PENDING); |
| 2664 | TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn); |
| 2665 | goto out; |
| 2666 | } |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 2667 | ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc])); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2668 | if (unlikely(ret <= 0)) { |
| 2669 | if (!ret) { |
| 2670 | h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ERR_PENDING); |
| 2671 | TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn); |
| 2672 | goto out; |
| 2673 | } |
| 2674 | else { |
| 2675 | /* we cannot report this error, so claim a success */ |
| 2676 | ret = 1; |
| 2677 | } |
| 2678 | } |
| 2679 | h1c->flags &= ~H1C_F_ERR_PENDING; |
| 2680 | out: |
| 2681 | TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn); |
| 2682 | return ret; |
| 2683 | } |
| 2684 | |
| 2685 | /* Try to send a 500 internal error. It relies on h1_send_error to send the |
| 2686 | * error. This function takes care of incrementing stats and tracked counters. |
| 2687 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2688 | static int h1_handle_internal_err(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2689 | { |
| 2690 | struct session *sess = h1c->conn->owner; |
| 2691 | int ret = 1; |
| 2692 | |
| 2693 | session_inc_http_req_ctr(sess); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2694 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2695 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]); |
| 2696 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2697 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2698 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2699 | |
| 2700 | h1c->errcode = 500; |
| 2701 | ret = h1_send_error(h1c); |
| 2702 | sess_log(sess); |
| 2703 | return ret; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2706 | /* Try to send an error because of a parsing error. By default a 400 bad request |
| 2707 | * error is returned. But the status code may be specified by setting |
| 2708 | * h1c->errcode. It relies on h1_send_error to send the error. This function |
| 2709 | * takes care of incrementing stats and tracked counters. |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2710 | */ |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2711 | static int h1_handle_parsing_error(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2712 | { |
| 2713 | struct session *sess = h1c->conn->owner; |
| 2714 | int ret = 1; |
| 2715 | |
| 2716 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) |
| 2717 | goto end; |
| 2718 | |
| 2719 | session_inc_http_req_ctr(sess); |
| 2720 | session_inc_http_err_ctr(sess); |
| 2721 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2722 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2723 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2724 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2725 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2726 | |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2727 | if (!h1c->errcode) |
| 2728 | h1c->errcode = 400; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2729 | ret = h1_send_error(h1c); |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2730 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
| 2731 | sess_log(sess); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2732 | |
| 2733 | end: |
| 2734 | return ret; |
| 2735 | } |
| 2736 | |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2737 | /* Try to send a 501 not implemented error. It relies on h1_send_error to send |
| 2738 | * the error. This function takes care of incrementing stats and tracked |
| 2739 | * counters. |
| 2740 | */ |
| 2741 | static int h1_handle_not_impl_err(struct h1c *h1c) |
| 2742 | { |
| 2743 | struct session *sess = h1c->conn->owner; |
| 2744 | int ret = 1; |
| 2745 | |
| 2746 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) |
| 2747 | goto end; |
| 2748 | |
| 2749 | session_inc_http_req_ctr(sess); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2750 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2751 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2752 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2753 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2754 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2755 | |
| 2756 | h1c->errcode = 501; |
| 2757 | ret = h1_send_error(h1c); |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2758 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
| 2759 | sess_log(sess); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2760 | |
| 2761 | end: |
| 2762 | return ret; |
| 2763 | } |
| 2764 | |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2765 | /* Try to send a 408 timeout error. It relies on h1_send_error to send the |
| 2766 | * error. This function takes care of incrementing stats and tracked counters. |
| 2767 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2768 | static int h1_handle_req_tout(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2769 | { |
| 2770 | struct session *sess = h1c->conn->owner; |
| 2771 | int ret = 1; |
| 2772 | |
| 2773 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) |
| 2774 | goto end; |
| 2775 | |
| 2776 | session_inc_http_req_ctr(sess); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2777 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2778 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2779 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2780 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2781 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2782 | |
| 2783 | h1c->errcode = 408; |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2784 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
| 2785 | ret = h1_send_error(h1c); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2786 | sess_log(sess); |
| 2787 | end: |
| 2788 | return ret; |
| 2789 | } |
| 2790 | |
| 2791 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2792 | /* |
| 2793 | * Attempt to read data, and subscribe if none available |
| 2794 | */ |
| 2795 | static int h1_recv(struct h1c *h1c) |
| 2796 | { |
| 2797 | struct connection *conn = h1c->conn; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2798 | size_t ret = 0, max; |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2799 | int flags = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2800 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2801 | TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn); |
| 2802 | |
| 2803 | if (h1c->wait_event.events & SUB_RETRY_RECV) { |
| 2804 | TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 2805 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2806 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2807 | |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2808 | if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) { |
| 2809 | TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2810 | return 1; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2811 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2812 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 2813 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 2814 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2815 | TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2816 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2817 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2818 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 2819 | /* |
| 2820 | * If we only have a small amount of data, realign it, |
| 2821 | * it's probably cheaper than doing 2 recv() calls. |
| 2822 | */ |
| 2823 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
Christopher Faulet | 00d7cde | 2021-02-04 11:01:51 +0100 | [diff] [blame] | 2824 | b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx)); |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 2825 | |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2826 | /* avoid useless reads after first responses */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2827 | if (!h1c->h1s || |
| 2828 | (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) || |
| 2829 | ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE)) |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2830 | flags |= CO_RFL_READ_ONCE; |
| 2831 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 2832 | max = buf_room_for_htx_data(&h1c->ibuf); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2833 | if (max) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2834 | if (h1c->flags & H1C_F_IN_FULL) { |
| 2835 | h1c->flags &= ~H1C_F_IN_FULL; |
| 2836 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2837 | } |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2838 | |
| 2839 | if (!b_data(&h1c->ibuf)) { |
| 2840 | /* try to pre-align the buffer like the rxbufs will be |
| 2841 | * to optimize memory copies. |
| 2842 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2843 | h1c->ibuf.head = sizeof(struct htx); |
| 2844 | } |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2845 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags); |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 2846 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2847 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2848 | if (max && !ret && h1_recv_allowed(h1c)) { |
| 2849 | TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn); |
| 2850 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2851 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2852 | else { |
| 2853 | h1_wake_stream_for_recv(h1c->h1s); |
| 2854 | TRACE_DATA("data received", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret}); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2857 | if (!b_data(&h1c->ibuf)) |
| 2858 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2859 | else if (!buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2860 | h1c->flags |= H1C_F_IN_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2861 | TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2862 | } |
| 2863 | |
| 2864 | TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2865 | return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | |
| 2869 | /* |
| 2870 | * Try to send data if possible |
| 2871 | */ |
| 2872 | static int h1_send(struct h1c *h1c) |
| 2873 | { |
| 2874 | struct connection *conn = h1c->conn; |
| 2875 | unsigned int flags = 0; |
| 2876 | size_t ret; |
| 2877 | int sent = 0; |
| 2878 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2879 | TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn); |
| 2880 | |
| 2881 | if (conn->flags & CO_FL_ERROR) { |
| 2882 | TRACE_DEVEL("leaving on connection error", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2883 | b_reset(&h1c->obuf); |
| 2884 | return 1; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2885 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2886 | |
| 2887 | if (!b_data(&h1c->obuf)) |
| 2888 | goto end; |
| 2889 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2890 | if (h1c->flags & H1C_F_CO_MSG_MORE) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2891 | flags |= CO_SFL_MSG_MORE; |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2892 | if (h1c->flags & H1C_F_CO_STREAMER) |
| 2893 | flags |= CO_SFL_STREAMER; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2894 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2895 | 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] | 2896 | if (ret > 0) { |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2897 | TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2898 | if (h1c->flags & H1C_F_OUT_FULL) { |
| 2899 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 2900 | TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn); |
| 2901 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 2902 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2903 | b_del(&h1c->obuf, ret); |
| 2904 | sent = 1; |
| 2905 | } |
| 2906 | |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2907 | if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2908 | 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] | 2909 | /* error or output closed, nothing to send, clear the buffer to release it */ |
| 2910 | b_reset(&h1c->obuf); |
| 2911 | } |
| 2912 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2913 | end: |
Christopher Faulet | c17c31c | 2022-02-01 18:25:06 +0100 | [diff] [blame] | 2914 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2915 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2916 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2917 | /* We're done, no more to send */ |
| 2918 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2919 | TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2920 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 2921 | if (h1c->flags & H1C_F_ST_SHUTDOWN) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2922 | TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 2923 | h1_shutw_conn(conn); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2924 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2925 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2926 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) { |
| 2927 | 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] | 2928 | 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] | 2929 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2930 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2931 | TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2932 | return sent; |
| 2933 | } |
| 2934 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2935 | /* callback called on any event by the connection handler. |
| 2936 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2937 | * destruction of the connection. |
| 2938 | */ |
| 2939 | static int h1_process(struct h1c * h1c) |
| 2940 | { |
| 2941 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2942 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2943 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2944 | TRACE_ENTER(H1_EV_H1C_WAKE, conn); |
| 2945 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2946 | /* Try to parse now the first block of a request, creating the H1 stream if necessary */ |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 2947 | if (b_data(&h1c->ibuf) && /* Input data to be processed */ |
Christopher Faulet | ab7389d | 2021-09-16 08:16:23 +0200 | [diff] [blame] | 2948 | (h1c->flags & H1C_F_ST_ALIVE) && !(h1c->flags & H1C_F_ST_READY) && /* ST_IDLE/ST_EMBRYONIC or ST_ATTACH but not ST_READY */ |
| 2949 | !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ST_ERROR))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2950 | struct buffer *buf; |
| 2951 | size_t count; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2952 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2953 | /* When it happens for a backend connection, we may release it (it is probably a 408) */ |
| 2954 | if (h1c->flags & H1C_F_IS_BACK) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2955 | goto release; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2956 | |
| 2957 | /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */ |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 2958 | if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */ |
Christopher Faulet | 143e9e5 | 2021-03-08 15:32:03 +0100 | [diff] [blame] | 2959 | !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */ |
| 2960 | !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2961 | /* Try to match H2 preface before parsing the request headers. */ |
| 2962 | if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) { |
| 2963 | h1c->flags |= H1C_F_UPG_H2C; |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 2964 | if (h1c->flags & H1C_F_ST_ATTACHED) { |
| 2965 | /* Force the REOS here to be sure to release the CS. |
| 2966 | Here ATTACHED implies !READY, and h1s defined |
| 2967 | */ |
| 2968 | BUG_ON(!h1s || (h1c->flags & H1C_F_ST_READY)); |
| 2969 | h1s->flags |= H1S_F_REOS; |
| 2970 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2971 | TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2972 | goto release; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2973 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2974 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2975 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2976 | /* Create the H1 stream if not already there */ |
| 2977 | if (!h1s) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 2978 | h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2979 | if (!h1s) { |
| 2980 | b_reset(&h1c->ibuf); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 2981 | h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2982 | goto no_parsing; |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | if (h1s->sess->t_idle == -1) |
| 2987 | h1s->sess->t_idle = tv_ms_elapsed(&h1s->sess->tv_accept, &now) - h1s->sess->t_handshake; |
| 2988 | |
| 2989 | /* Get the stream rxbuf */ |
| 2990 | buf = h1_get_buf(h1c, &h1s->rxbuf); |
| 2991 | if (!buf) { |
| 2992 | h1c->flags |= H1C_F_IN_SALLOC; |
| 2993 | TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn); |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
| 2997 | count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 2998 | h1_process_demux(h1c, buf, count); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2999 | h1_release_buf(h1c, &h1s->rxbuf); |
| 3000 | h1_set_idle_expiration(h1c); |
| 3001 | |
| 3002 | no_parsing: |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3003 | if (h1c->flags & H1C_F_ST_ERROR) { |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3004 | h1_handle_internal_err(h1c); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3005 | h1c->flags &= ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3006 | TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3007 | } |
| 3008 | else if (h1s->flags & H1S_F_PARSING_ERROR) { |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 3009 | h1_handle_parsing_error(h1c); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3010 | h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3011 | TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3012 | } |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 3013 | else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) { |
| 3014 | h1_handle_not_impl_err(h1c); |
| 3015 | h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3016 | TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 3017 | } |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 3018 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3019 | h1_send(h1c); |
| 3020 | |
Christopher Faulet | 7530830 | 2021-11-15 14:51:37 +0100 | [diff] [blame] | 3021 | /* H1 connection must be released ASAP if: |
| 3022 | * - an error occurred on the connection or the H1C or |
| 3023 | * - a read0 was received or |
| 3024 | * - a silent shutdown was emitted and all outgoing data sent |
| 3025 | */ |
| 3026 | if ((conn->flags & CO_FL_ERROR) || |
| 3027 | conn_xprt_read0_pending(conn) || |
| 3028 | (h1c->flags & H1C_F_ST_ERROR) || |
| 3029 | ((h1c->flags & H1C_F_ST_SILENT_SHUT) && !b_data(&h1c->obuf))) { |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3030 | if (!(h1c->flags & H1C_F_ST_READY)) { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3031 | /* No stream connector or not ready */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3032 | /* shutdown for reads and error on the frontend connection: Send an error */ |
Christopher Faulet | 7530830 | 2021-11-15 14:51:37 +0100 | [diff] [blame] | 3033 | if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ST_SHUTDOWN))) { |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 3034 | if (h1_handle_parsing_error(h1c)) |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3035 | h1_send(h1c); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3036 | h1c->flags = (h1c->flags & ~(H1C_F_ST_IDLE|H1C_F_WAIT_NEXT_REQ)) | H1C_F_ST_ERROR; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | /* Handle pending error, if any (only possible on frontend connection) */ |
| 3040 | if (h1c->flags & H1C_F_ERR_PENDING) { |
| 3041 | BUG_ON(h1c->flags & H1C_F_IS_BACK); |
| 3042 | if (h1_send_error(h1c)) |
| 3043 | h1_send(h1c); |
| 3044 | } |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 3045 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3046 | /* If there is some pending outgoing data or error, just wait */ |
| 3047 | if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) |
| 3048 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 3049 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3050 | /* Otherwise we can release the H1 connection */ |
| 3051 | goto release; |
| 3052 | } |
| 3053 | else { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3054 | /* Here there is still a H1 stream with a stream connector. |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3055 | * Report the connection state at the stream level |
| 3056 | */ |
| 3057 | if (conn_xprt_read0_pending(conn)) { |
| 3058 | h1s->flags |= H1S_F_REOS; |
| 3059 | TRACE_STATE("read0 on connection", H1_EV_H1C_RECV, conn, h1s); |
| 3060 | } |
Willy Tarreau | 99bbdbc | 2022-03-17 17:10:36 +0100 | [diff] [blame] | 3061 | if ((h1c->flags & H1C_F_ST_ERROR) || ((conn->flags & CO_FL_ERROR) && !b_data(&h1c->ibuf))) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3062 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3063 | TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 3064 | h1_alert(h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3065 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3066 | } |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 3067 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3068 | if (!b_data(&h1c->ibuf)) |
| 3069 | h1_release_buf(h1c, &h1c->ibuf); |
| 3070 | |
Amaury Denoyelle | efc6e95 | 2021-05-05 11:11:11 +0200 | [diff] [blame] | 3071 | /* Check if a soft-stop is in progress. |
| 3072 | * Release idling front connection if this is the case. |
| 3073 | */ |
| 3074 | if (!(h1c->flags & H1C_F_IS_BACK)) { |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 3075 | if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
William Dauchy | a9dd901 | 2022-01-05 22:53:24 +0100 | [diff] [blame] | 3076 | if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) && |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 3077 | h1c->flags & H1C_F_WAIT_NEXT_REQ) { |
| 3078 | |
| 3079 | int send_close = 1; |
| 3080 | /* If a close-spread-time option is set, we want to avoid |
| 3081 | * closing all the active HTTP2 connections at once so we add a |
| 3082 | * random factor that will spread the closing. |
| 3083 | */ |
| 3084 | if (tick_isset(global.close_spread_end)) { |
| 3085 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 3086 | if (remaining_window) { |
| 3087 | /* This should increase the closing rate the |
| 3088 | * further along the window we are. |
| 3089 | */ |
| 3090 | send_close = (remaining_window <= statistical_prng_range(global.close_spread_time)); |
| 3091 | } |
| 3092 | } |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 3093 | else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) |
| 3094 | send_close = 0; /* let the client close his connection himself */ |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 3095 | if (send_close) |
| 3096 | goto release; |
| 3097 | } |
Amaury Denoyelle | efc6e95 | 2021-05-05 11:11:11 +0200 | [diff] [blame] | 3098 | } |
| 3099 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3100 | |
| 3101 | if ((h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1s)) { |
| 3102 | TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn); |
| 3103 | h1_wake_stream_for_recv(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3104 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3105 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 3106 | end: |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3107 | h1_refresh_timeout(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3108 | TRACE_LEAVE(H1_EV_H1C_WAKE, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3109 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3110 | |
| 3111 | release: |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3112 | if (h1c->flags & H1C_F_ST_ATTACHED) { |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 3113 | /* Don't release the H1 connection right now, we must destroy the |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3114 | * attached CS first. Here, the H1C must not be READY */ |
| 3115 | BUG_ON(!h1s || h1c->flags & H1C_F_ST_READY); |
| 3116 | |
| 3117 | if (conn_xprt_read0_pending(conn) || (h1s->flags & H1S_F_REOS)) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3118 | se_fl_set(h1s->endp, SE_FL_EOS); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3119 | if ((h1c->flags & H1C_F_ST_ERROR) || (conn->flags & CO_FL_ERROR)) |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3120 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3121 | h1_alert(h1s); |
| 3122 | TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE); |
| 3123 | } |
| 3124 | else { |
| 3125 | h1_release(h1c); |
| 3126 | TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE); |
| 3127 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3128 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3129 | } |
| 3130 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3131 | struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3132 | { |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3133 | struct connection *conn; |
| 3134 | struct tasklet *tl = (struct tasklet *)t; |
| 3135 | int conn_in_list; |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3136 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3137 | int ret = 0; |
| 3138 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3139 | if (state & TASK_F_USR1) { |
| 3140 | /* the tasklet was idling on an idle connection, it might have |
| 3141 | * been stolen, let's be careful! |
| 3142 | */ |
| 3143 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 3144 | if (tl->context == NULL) { |
| 3145 | /* The connection has been taken over by another thread, |
| 3146 | * we're no longer responsible for it, so just free the |
| 3147 | * tasklet, and do nothing. |
| 3148 | */ |
| 3149 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 3150 | tasklet_free(tl); |
| 3151 | return NULL; |
| 3152 | } |
| 3153 | conn = h1c->conn; |
| 3154 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3156 | /* Remove the connection from the list, to be sure nobody attempts |
| 3157 | * to use it while we handle the I/O events |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3158 | */ |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3159 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 3160 | if (conn_in_list) |
| 3161 | conn_delete_from_tree(&conn->hash_node->node); |
| 3162 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3163 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3164 | } else { |
| 3165 | /* we're certain the connection was not in an idle list */ |
| 3166 | conn = h1c->conn; |
| 3167 | TRACE_ENTER(H1_EV_H1C_WAKE, conn); |
| 3168 | conn_in_list = 0; |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3169 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3170 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3171 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3172 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3173 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3174 | ret |= h1_recv(h1c); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3175 | if (ret || b_data(&h1c->ibuf)) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3176 | ret = h1_process(h1c); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3177 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3178 | /* If we were in an idle list, we want to add it back into it, |
| 3179 | * unless h1_process() returned -1, which mean it has destroyed |
| 3180 | * the connection (testing !ret is enough, if h1_process() wasn't |
| 3181 | * called then ret will be 0 anyway. |
| 3182 | */ |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3183 | if (ret < 0) |
| 3184 | t = NULL; |
| 3185 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3186 | if (!ret && conn_in_list) { |
| 3187 | struct server *srv = objt_server(conn->target); |
| 3188 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3189 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3190 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 3191 | ebmb_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3192 | else |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 3193 | ebmb_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash)); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3194 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3195 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3196 | return t; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3199 | static int h1_wake(struct connection *conn) |
| 3200 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3201 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3202 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3203 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3204 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
| 3205 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3206 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3207 | ret = h1_process(h1c); |
| 3208 | if (ret == 0) { |
| 3209 | struct h1s *h1s = h1c->h1s; |
| 3210 | |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 3211 | if (h1c->flags & H1C_F_ST_ATTACHED) |
| 3212 | h1_alert(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3213 | } |
| 3214 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3215 | } |
| 3216 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3217 | /* Connection timeout management. The principle is that if there's no receipt |
| 3218 | * nor sending for a certain amount of time, the connection is closed. |
| 3219 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3220 | struct task *h1_timeout_task(struct task *t, void *context, unsigned int state) |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3221 | { |
| 3222 | struct h1c *h1c = context; |
| 3223 | int expired = tick_is_expired(t->expire, now_ms); |
| 3224 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3225 | TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3226 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3227 | if (h1c) { |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3228 | /* Make sure nobody stole the connection from us */ |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3229 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3230 | |
| 3231 | /* Somebody already stole the connection from us, so we should not |
| 3232 | * free it, we just have to free the task. |
| 3233 | */ |
| 3234 | if (!t->context) { |
| 3235 | h1c = NULL; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3236 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3237 | goto do_leave; |
| 3238 | } |
| 3239 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3240 | if (!expired) { |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3241 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3242 | TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s); |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3243 | return t; |
| 3244 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3245 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3246 | /* If a stream connector is still attached and ready to the mux, wait for the |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3247 | * stream's timeout |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3248 | */ |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3249 | if (h1c->flags & H1C_F_ST_READY) { |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3250 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3251 | t->expire = TICK_ETERNITY; |
| 3252 | TRACE_DEVEL("leaving (CS still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s); |
| 3253 | return t; |
| 3254 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3255 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3256 | /* Try to send an error to the client */ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3257 | if (!(h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_ERROR|H1C_F_ERR_PENDING|H1C_F_ST_SHUTDOWN))) { |
| 3258 | h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3259 | TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3260 | if (h1_handle_req_tout(h1c)) |
| 3261 | h1_send(h1c); |
| 3262 | if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ERR_PENDING)) { |
| 3263 | h1_refresh_timeout(h1c); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3264 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3265 | return t; |
| 3266 | } |
| 3267 | } |
| 3268 | |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3269 | if (h1c->flags & H1C_F_ST_ATTACHED) { |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 3270 | /* Don't release the H1 connection right now, we must destroy the |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3271 | * attached CS first. Here, the H1C must not be READY */ |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3272 | se_fl_set(h1c->h1s->endp, SE_FL_EOS | SE_FL_ERROR); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3273 | h1_alert(h1c->h1s); |
| 3274 | h1_refresh_timeout(h1c); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3275 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3276 | TRACE_DEVEL("waiting to release the CS before releasing the connection", H1_EV_H1C_WAKE); |
| 3277 | return t; |
| 3278 | } |
| 3279 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3280 | /* We're about to destroy the connection, so make sure nobody attempts |
| 3281 | * to steal it from us. |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3282 | */ |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3283 | if (h1c->conn->flags & CO_FL_LIST_MASK) |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 3284 | conn_delete_from_tree(&h1c->conn->hash_node->node); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3285 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3286 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3287 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3288 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3289 | do_leave: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3290 | task_destroy(t); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3291 | |
| 3292 | if (!h1c) { |
| 3293 | /* resources were already deleted */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3294 | TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3295 | return NULL; |
| 3296 | } |
| 3297 | |
| 3298 | h1c->task = NULL; |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3299 | h1_release(h1c); |
| 3300 | TRACE_LEAVE(H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3301 | return NULL; |
| 3302 | } |
| 3303 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3304 | /*******************************************/ |
| 3305 | /* functions below are used by the streams */ |
| 3306 | /*******************************************/ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3307 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3308 | /* |
| 3309 | * Attach a new stream to a connection |
| 3310 | * (Used for outgoing connections) |
| 3311 | */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 3312 | static int h1_attach(struct connection *conn, struct sedesc *endp, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3313 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3314 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3315 | struct h1s *h1s; |
| 3316 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3317 | TRACE_ENTER(H1_EV_STRM_NEW, conn); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3318 | if (h1c->flags & H1C_F_ST_ERROR) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3319 | TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
| 3320 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3321 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3322 | |
Willy Tarreau | c105492 | 2022-05-18 07:43:52 +0200 | [diff] [blame] | 3323 | h1s = h1c_bck_stream_new(h1c, endp->sc, sess); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3324 | if (h1s == NULL) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3325 | TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
| 3326 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3327 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3328 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3329 | /* the connection is not idle anymore, let's mark this */ |
| 3330 | HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1); |
Willy Tarreau | 4f8cd43 | 2021-03-02 17:27:58 +0100 | [diff] [blame] | 3331 | xprt_set_used(conn, conn->xprt, conn->xprt_ctx); |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3332 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3333 | TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s); |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 3334 | return 0; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3335 | err: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3336 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 3337 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3340 | /* Retrieves a valid stream connector from this connection, or returns NULL. |
| 3341 | * For this mux, it's easy as we can only store a single stream connector. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3342 | */ |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 3343 | static struct stconn *h1_get_first_sc(const struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3344 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3345 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3346 | struct h1s *h1s = h1c->h1s; |
| 3347 | |
| 3348 | if (h1s) |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 3349 | return h1s_sc(h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3350 | |
| 3351 | return NULL; |
| 3352 | } |
| 3353 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3354 | static void h1_destroy(void *ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3355 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3356 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3357 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3358 | TRACE_POINT(H1_EV_H1C_END, h1c->conn); |
Christopher Faulet | 7c452cc | 2022-04-14 11:08:26 +0200 | [diff] [blame] | 3359 | if (!h1c->h1s || h1c->conn->ctx != h1c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3360 | h1_release(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | /* |
| 3364 | * Detach the stream from the connection and possibly release the connection. |
| 3365 | */ |
Willy Tarreau | ea59b02 | 2022-05-17 17:53:22 +0200 | [diff] [blame] | 3366 | static void h1_detach(struct sedesc *endp) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3367 | { |
Willy Tarreau | 65d0597 | 2022-05-16 17:29:42 +0200 | [diff] [blame] | 3368 | struct h1s *h1s = endp->se; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3369 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3370 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3371 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3372 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3373 | TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s); |
| 3374 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3375 | if (!h1s) { |
| 3376 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3377 | return; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3378 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3379 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3380 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3381 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3382 | |
Christopher Faulet | 42849b0 | 2020-10-05 11:35:17 +0200 | [diff] [blame] | 3383 | sess->accept_date = date; |
| 3384 | sess->tv_accept = now; |
| 3385 | sess->t_handshake = 0; |
| 3386 | sess->t_idle = -1; |
| 3387 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3388 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 3389 | h1s_destroy(h1s); |
| 3390 | |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3391 | if ((h1c->flags & (H1C_F_IS_BACK|H1C_F_ST_IDLE)) == (H1C_F_IS_BACK|H1C_F_ST_IDLE)) { |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3392 | /* If there are any excess server data in the input buffer, |
| 3393 | * release it and close the connection ASAP (some data may |
| 3394 | * remain in the output buffer). This happens if a server sends |
| 3395 | * invalid responses. So in such case, we don't want to reuse |
| 3396 | * the connection |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 3397 | */ |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3398 | if (b_data(&h1c->ibuf)) { |
| 3399 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3400 | h1c->flags = (h1c->flags & ~H1C_F_ST_IDLE) | H1C_F_ST_SHUTDOWN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3401 | 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] | 3402 | goto release; |
| 3403 | } |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 3404 | |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3405 | if (h1c->conn->flags & CO_FL_PRIVATE) { |
| 3406 | /* Add the connection in the session server list, if not already done */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3407 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 3408 | h1c->conn->owner = NULL; |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3409 | h1c->conn->mux->destroy(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3410 | goto end; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3411 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3412 | /* Always idle at this step */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3413 | if (session_check_idle_conn(sess, h1c->conn)) { |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3414 | /* The connection got destroyed, let's leave */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3415 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 3416 | goto end; |
| 3417 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3418 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3419 | else { |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3420 | if (h1c->conn->owner == sess) |
| 3421 | h1c->conn->owner = NULL; |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3422 | |
| 3423 | /* mark that the tasklet may lose its context to another thread and |
| 3424 | * that the handler needs to check it under the idle conns lock. |
| 3425 | */ |
| 3426 | HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1); |
Olivier Houchard | 3c49c1b | 2020-03-22 19:56:03 +0100 | [diff] [blame] | 3427 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Willy Tarreau | 4f8cd43 | 2021-03-02 17:27:58 +0100 | [diff] [blame] | 3428 | xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx); |
| 3429 | |
Olivier Houchard | dc2f275 | 2020-02-13 19:12:07 +0100 | [diff] [blame] | 3430 | if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) { |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3431 | /* The server doesn't want it, let's kill the connection right away */ |
| 3432 | h1c->conn->mux->destroy(h1c); |
| 3433 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 3434 | goto end; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 3435 | } |
Olivier Houchard | 199d4fa | 2020-03-22 23:25:51 +0100 | [diff] [blame] | 3436 | /* At this point, the connection has been added to the |
| 3437 | * server idle list, so another thread may already have |
| 3438 | * hijacked it, so we can't do anything with it. |
| 3439 | */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3440 | return; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3444 | release: |
Christopher Faulet | 3ac0f43 | 2019-06-28 17:41:42 +0200 | [diff] [blame] | 3445 | /* We don't want to close right now unless the connection is in error or shut down for writes */ |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3446 | if ((h1c->flags & H1C_F_ST_ERROR) || |
Christopher Faulet | 37243bc | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 3447 | (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) || |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3448 | ((h1c->flags & H1C_F_ST_SHUTDOWN) && !b_data(&h1c->obuf)) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3449 | !h1c->conn->owner) { |
| 3450 | TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn); |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3451 | h1_release(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3452 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3453 | else { |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3454 | if (h1c->flags & H1C_F_ST_IDLE) { |
Christopher Faulet | 5d3c93c | 2020-10-05 17:14:28 +0200 | [diff] [blame] | 3455 | /* If we have a new request, process it immediately or |
| 3456 | * subscribe for reads waiting for new data |
| 3457 | */ |
Christopher Faulet | 0c366a8 | 2020-12-18 15:13:47 +0100 | [diff] [blame] | 3458 | if (unlikely(b_data(&h1c->ibuf))) { |
| 3459 | if (h1_process(h1c) == -1) |
| 3460 | goto end; |
| 3461 | } |
Christopher Faulet | 5d3c93c | 2020-10-05 17:14:28 +0200 | [diff] [blame] | 3462 | else |
| 3463 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 3464 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3465 | h1_set_idle_expiration(h1c); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3466 | h1_refresh_timeout(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3467 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3468 | end: |
| 3469 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3473 | static void h1_shutr(struct stconn *sc, enum co_shr_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3474 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3475 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3476 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3477 | |
| 3478 | if (!h1s) |
| 3479 | return; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3480 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3481 | |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 3482 | TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3483 | |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3484 | if (se_fl_test(h1s->endp, SE_FL_SHR)) |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 3485 | goto end; |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3486 | if (se_fl_test(h1s->endp, SE_FL_KILL_CONN)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3487 | TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 3488 | goto do_shutr; |
| 3489 | } |
| 3490 | if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
| 3491 | 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] | 3492 | goto do_shutr; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3493 | } |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3494 | |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3495 | if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) { |
| 3496 | /* Here attached is implicit because there is CS */ |
| 3497 | TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 3498 | goto end; |
| 3499 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3500 | if (h1s->flags & H1S_F_WANT_KAL) { |
| 3501 | TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3502 | goto end; |
| 3503 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 3504 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3505 | do_shutr: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3506 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3507 | if (se_fl_test(h1s->endp, SE_FL_SHR)) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3508 | goto end; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3509 | |
| 3510 | if (conn_xprt_ready(h1c->conn) && h1c->conn->xprt->shutr) |
Christopher Faulet | 0797656 | 2022-03-31 11:05:05 +0200 | [diff] [blame] | 3511 | h1c->conn->xprt->shutr(h1c->conn, h1c->conn->xprt_ctx, (mode == CO_SHR_DRAIN)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3512 | end: |
| 3513 | TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3514 | } |
| 3515 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3516 | static void h1_shutw(struct stconn *sc, enum co_shw_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3517 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3518 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3519 | struct h1c *h1c; |
| 3520 | |
| 3521 | if (!h1s) |
| 3522 | return; |
| 3523 | h1c = h1s->h1c; |
| 3524 | |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 3525 | TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3526 | |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3527 | if (se_fl_test(h1s->endp, SE_FL_SHW)) |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 3528 | goto end; |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3529 | if (se_fl_test(h1s->endp, SE_FL_KILL_CONN)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3530 | 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] | 3531 | goto do_shutw; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3532 | } |
| 3533 | if (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) { |
| 3534 | TRACE_STATE("shutdown on connection (error|rd_sh|wr_sh)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 3535 | goto do_shutw; |
| 3536 | } |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 3537 | |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3538 | if (!(h1c->flags & (H1C_F_ST_READY|H1C_F_ST_ERROR))) { |
| 3539 | /* Here attached is implicit because there is CS */ |
| 3540 | TRACE_STATE("keep connection alive (ALIVE but not READY nor ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
| 3541 | goto end; |
| 3542 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3543 | if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) { |
| 3544 | TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3545 | goto end; |
| 3546 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 3547 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3548 | do_shutw: |
Christopher Faulet | 3ced1d1 | 2020-11-27 16:44:01 +0100 | [diff] [blame] | 3549 | h1c->flags |= H1C_F_ST_SHUTDOWN; |
Christopher Faulet | 0797656 | 2022-03-31 11:05:05 +0200 | [diff] [blame] | 3550 | if (mode != CO_SHW_NORMAL) |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3551 | h1c->flags |= H1C_F_ST_SILENT_SHUT; |
| 3552 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 3553 | if (!b_data(&h1c->obuf)) |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3554 | h1_shutw_conn(h1c->conn); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3555 | end: |
| 3556 | TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3557 | } |
| 3558 | |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3559 | static void h1_shutw_conn(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3560 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3561 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3562 | |
Willy Tarreau | 62592ad | 2021-03-26 09:09:42 +0100 | [diff] [blame] | 3563 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 3564 | return; |
| 3565 | |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3566 | TRACE_ENTER(H1_EV_H1C_END, conn); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 3567 | conn_xprt_shutw(conn); |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3568 | conn_sock_shutw(conn, (h1c && !(h1c->flags & H1C_F_ST_SILENT_SHUT))); |
| 3569 | TRACE_LEAVE(H1_EV_H1C_END, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3570 | } |
| 3571 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3572 | /* Called from the upper layer, to unsubscribe <es> from events <event_type> |
| 3573 | * The <es> pointer is not allowed to differ from the one passed to the |
| 3574 | * subscribe() call. It always returns zero. |
| 3575 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3576 | static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3577 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3578 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3579 | |
| 3580 | if (!h1s) |
| 3581 | return 0; |
| 3582 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3583 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3584 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3585 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3586 | es->events &= ~event_type; |
| 3587 | if (!es->events) |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3588 | h1s->subs = NULL; |
| 3589 | |
| 3590 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3591 | TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3592 | |
| 3593 | if (event_type & SUB_RETRY_SEND) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3594 | TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3595 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3596 | return 0; |
| 3597 | } |
| 3598 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3599 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 3600 | * event subscriber <es> is not allowed to change from a previous call as long |
| 3601 | * as at least one event is still subscribed. The <event_type> must only be a |
| 3602 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3603 | * the stream connector <sc> was already detached, in which case it will return |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3604 | * -1. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3605 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3606 | static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3607 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3608 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51bb185 | 2019-09-04 10:22:34 +0200 | [diff] [blame] | 3609 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3610 | |
| 3611 | if (!h1s) |
| 3612 | return -1; |
| 3613 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3614 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3615 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3616 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3617 | es->events |= event_type; |
| 3618 | h1s->subs = es; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3619 | |
| 3620 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3621 | TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3622 | |
| 3623 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3624 | if (event_type & SUB_RETRY_SEND) { |
| 3625 | TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3626 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3627 | * If the stconn attempts to subscribe, and the |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3628 | * mux isn't subscribed to the connection, then it |
| 3629 | * probably means the connection wasn't established |
| 3630 | * yet, so we have to subscribe. |
| 3631 | */ |
| 3632 | h1c = h1s->h1c; |
| 3633 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
| 3634 | h1c->conn->xprt->subscribe(h1c->conn, |
| 3635 | h1c->conn->xprt_ctx, |
| 3636 | SUB_RETRY_SEND, |
| 3637 | &h1c->wait_event); |
| 3638 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3639 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3640 | } |
| 3641 | |
Christopher Faulet | 564e39c | 2021-09-21 15:50:55 +0200 | [diff] [blame] | 3642 | /* Called from the upper layer, to receive data. |
| 3643 | * |
| 3644 | * The caller is responsible for defragmenting <buf> if necessary. But <flags> |
| 3645 | * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it |
| 3646 | * means the caller wants to flush input data (from the mux buffer and the |
| 3647 | * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux |
| 3648 | * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read |
| 3649 | * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with |
| 3650 | * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the |
| 3651 | * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should |
| 3652 | * copy as much data as possible. |
| 3653 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3654 | static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3655 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3656 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3657 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3658 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3659 | size_t ret = 0; |
| 3660 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3661 | TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3662 | |
| 3663 | /* Do nothing for now if not READY */ |
| 3664 | if (!(h1c->flags & H1C_F_ST_READY)) { |
| 3665 | TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
| 3666 | goto end; |
| 3667 | } |
| 3668 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3669 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 3670 | ret = h1_process_demux(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3671 | else |
| 3672 | 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] | 3673 | |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3674 | if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->endp, SE_FL_MAY_SPLICE)) { |
Christopher Faulet | 2b861bf | 2021-04-06 17:24:39 +0200 | [diff] [blame] | 3675 | h1c->flags |= H1C_F_WANT_SPLICE; |
| 3676 | TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3677 | } |
Olivier Houchard | 02bac85 | 2019-08-22 18:34:25 +0200 | [diff] [blame] | 3678 | else { |
Christopher Faulet | 1baef15 | 2021-04-08 18:42:59 +0200 | [diff] [blame] | 3679 | if (((flags & CO_RFL_KEEP_RECV) || (h1m->state != H1_MSG_DONE)) && !(h1c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 3680 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3681 | } |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3682 | |
| 3683 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3684 | TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3685 | return ret; |
| 3686 | } |
| 3687 | |
| 3688 | |
| 3689 | /* Called from the upper layer, to send data */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3690 | static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3691 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3692 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3693 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3694 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3697 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3698 | h1c = h1s->h1c; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 3699 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3700 | TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3701 | |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 3702 | /* If we're not connected yet, or we're waiting for a handshake, stop |
| 3703 | * now, as we don't want to remove everything from the channel buffer |
| 3704 | * before we're sure we can send it. |
| 3705 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3706 | if (h1c->conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3707 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 3708 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3709 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3710 | |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3711 | if (h1c->flags & H1C_F_ST_ERROR) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3712 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3713 | TRACE_ERROR("H1C on error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3714 | return 0; |
| 3715 | } |
| 3716 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 3717 | /* Inherit some flags from the upper layer */ |
| 3718 | h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
| 3719 | if (flags & CO_SFL_MSG_MORE) |
| 3720 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 3721 | if (flags & CO_SFL_STREAMER) |
| 3722 | h1c->flags |= H1C_F_CO_STREAMER; |
| 3723 | |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 3724 | while (count) { |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3725 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3726 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3727 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 3728 | ret = h1_process_mux(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3729 | else |
| 3730 | TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s); |
Olivier Houchard | c89a42f | 2020-06-19 16:15:05 +0200 | [diff] [blame] | 3731 | |
| 3732 | if ((count - ret) > 0) |
| 3733 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 3734 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3735 | if (!ret) |
| 3736 | break; |
| 3737 | total += ret; |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 3738 | count -= ret; |
Olivier Houchard | 68787ef | 2020-01-15 19:13:32 +0100 | [diff] [blame] | 3739 | if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c)) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3740 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3741 | } |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3742 | |
| 3743 | if (h1c->flags & H1C_F_ST_ERROR) { |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3744 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3745 | TRACE_ERROR("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3748 | h1_refresh_timeout(h1c); |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3749 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total}); |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3750 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3751 | } |
| 3752 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 3753 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3754 | /* Send and get, using splicing */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3755 | static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3756 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3757 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3758 | struct h1c *h1c = h1s->h1c; |
| 3759 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3760 | int ret = 0; |
| 3761 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3762 | TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3763 | |
Christopher Faulet | 9fa40c4 | 2019-11-05 16:24:27 +0100 | [diff] [blame] | 3764 | if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3765 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3766 | TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3767 | goto end; |
| 3768 | } |
| 3769 | |
Christopher Faulet | cf30756 | 2021-07-26 10:49:39 +0200 | [diff] [blame] | 3770 | h1c->flags |= H1C_F_WANT_SPLICE; |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 3771 | if (h1s_data_pending(h1s)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3772 | TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3773 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 3774 | } |
| 3775 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3776 | if (!h1_recv_allowed(h1c)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3777 | TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 0060be9 | 2020-07-03 15:02:25 +0200 | [diff] [blame] | 3778 | goto end; |
| 3779 | } |
| 3780 | |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3781 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3782 | count = h1m->curr_len; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3783 | ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3784 | if (ret >= 0) { |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3785 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3786 | if (ret > h1m->curr_len) { |
| 3787 | h1s->flags |= H1S_F_PARSING_ERROR; |
| 3788 | h1c->flags |= H1C_F_ST_ERROR; |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3789 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3790 | TRACE_ERROR("too much payload, more than announced", |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3791 | H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3792 | goto end; |
| 3793 | } |
| 3794 | h1m->curr_len -= ret; |
| 3795 | if (!h1m->curr_len) { |
| 3796 | h1m->state = H1_MSG_DONE; |
| 3797 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3798 | TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3799 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3800 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 3801 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret); |
| 3802 | HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3805 | end: |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3806 | if (conn_xprt_read0_pending(h1c->conn)) { |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 3807 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3808 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3809 | TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 3810 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3811 | |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3812 | if (!(h1c->flags & H1C_F_WANT_SPLICE)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3813 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3814 | se_fl_clr(h1s->endp, SE_FL_MAY_SPLICE); |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3815 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3816 | TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s); |
| 3817 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3818 | } |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 3819 | } |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 3820 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3821 | TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3822 | return ret; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3825 | static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3826 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3827 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3828 | struct h1c *h1c = h1s->h1c; |
| 3829 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3830 | int ret = 0; |
| 3831 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3832 | TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3833 | |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3834 | if (b_data(&h1c->obuf)) { |
| 3835 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3836 | TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s); |
| 3837 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3838 | } |
Christopher Faulet | 8454f2d | 2021-04-06 17:27:32 +0200 | [diff] [blame] | 3839 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 3840 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3841 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3842 | ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe); |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3843 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3844 | if (ret > h1m->curr_len) { |
| 3845 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
| 3846 | h1c->flags |= H1C_F_ST_ERROR; |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3847 | se_fl_set(h1s->endp, SE_FL_ERROR); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3848 | TRACE_ERROR("too much payload, more than announced", |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3849 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3850 | goto end; |
| 3851 | } |
| 3852 | h1m->curr_len -= ret; |
| 3853 | if (!h1m->curr_len) { |
| 3854 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3855 | TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3856 | } |
| 3857 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 3858 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret); |
| 3859 | HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret); |
Christopher Faulet | 8454f2d | 2021-04-06 17:27:32 +0200 | [diff] [blame] | 3860 | |
| 3861 | end: |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3862 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3863 | return ret; |
| 3864 | } |
| 3865 | #endif |
| 3866 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3867 | static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3868 | { |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 3869 | const struct h1c *h1c = conn->ctx; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3870 | int ret = 0; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 3871 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3872 | switch (mux_ctl) { |
| 3873 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3874 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3875 | ret |= MUX_STATUS_READY; |
| 3876 | return ret; |
Christopher Faulet | 4c8ad84 | 2020-10-06 14:59:17 +0200 | [diff] [blame] | 3877 | case MUX_EXIT_STATUS: |
Christopher Faulet | 36e46aa | 2021-09-28 11:45:05 +0200 | [diff] [blame] | 3878 | if (output) |
| 3879 | *((int *)output) = h1c->errcode; |
| 3880 | ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR : |
| 3881 | (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR : |
| 3882 | (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR : |
| 3883 | ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR : |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 3884 | MUX_ES_SUCCESS)))); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 3885 | return ret; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3886 | default: |
| 3887 | return -1; |
| 3888 | } |
| 3889 | } |
| 3890 | |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3891 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 3892 | static int h1_show_fd(struct buffer *msg, struct connection *conn) |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3893 | { |
| 3894 | struct h1c *h1c = conn->ctx; |
| 3895 | struct h1s *h1s = h1c->h1s; |
Willy Tarreau | 0c0c0a2 | 2021-01-21 09:13:35 +0100 | [diff] [blame] | 3896 | int ret = 0; |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3897 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 3898 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 3899 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3900 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 3901 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
| 3902 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 3903 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 3904 | |
| 3905 | if (h1s) { |
| 3906 | char *method; |
| 3907 | |
| 3908 | if (h1s->meth < HTTP_METH_OTHER) |
| 3909 | method = http_known_methods[h1s->meth].ptr; |
| 3910 | else |
| 3911 | method = "UNKNOWN"; |
Christopher Faulet | 22050e0 | 2022-04-13 12:08:09 +0200 | [diff] [blame] | 3912 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .endp.flg=0x%x .req.state=%s .res.state=%s" |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3913 | " .meth=%s status=%d", |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame] | 3914 | h1s, h1s->flags, se_fl_get(h1s->endp), |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3915 | h1m_state_str(h1s->req.state), |
| 3916 | h1m_state_str(h1s->res.state), method, h1s->status); |
Christopher Faulet | 22050e0 | 2022-04-13 12:08:09 +0200 | [diff] [blame] | 3917 | if (h1s->endp) { |
Willy Tarreau | d56377c | 2022-05-17 16:31:36 +0200 | [diff] [blame] | 3918 | chunk_appendf(msg, " .endp.flg=0x%08x", |
| 3919 | se_fl_get(h1s->endp)); |
Willy Tarreau | b605c42 | 2022-05-17 17:04:55 +0200 | [diff] [blame] | 3920 | if (!se_fl_test(h1s->endp, SE_FL_ORPHAN)) |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame^] | 3921 | chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p", |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 3922 | h1s_sc(h1s)->flags, h1s_sc(h1s)->app); |
Christopher Faulet | 22050e0 | 2022-04-13 12:08:09 +0200 | [diff] [blame] | 3923 | } |
Willy Tarreau | 150c4f8 | 2021-01-20 17:05:58 +0100 | [diff] [blame] | 3924 | chunk_appendf(&trash, " .subs=%p", h1s->subs); |
| 3925 | if (h1s->subs) { |
Christopher Faulet | 6c93c4e | 2021-02-25 10:06:29 +0100 | [diff] [blame] | 3926 | chunk_appendf(&trash, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet); |
| 3927 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 3928 | h1s->subs->tasklet->calls, |
| 3929 | h1s->subs->tasklet->context); |
| 3930 | if (h1s->subs->tasklet->calls >= 1000000) |
| 3931 | ret = 1; |
| 3932 | resolve_sym_name(&trash, NULL, h1s->subs->tasklet->process); |
| 3933 | chunk_appendf(&trash, ")"); |
Willy Tarreau | 150c4f8 | 2021-01-20 17:05:58 +0100 | [diff] [blame] | 3934 | } |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3935 | } |
Willy Tarreau | 0c0c0a2 | 2021-01-21 09:13:35 +0100 | [diff] [blame] | 3936 | return ret; |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | |
| 3940 | /* Add an entry in the headers map. Returns -1 on error and 0 on success. */ |
| 3941 | static int add_hdr_case_adjust(const char *from, const char *to, char **err) |
| 3942 | { |
| 3943 | struct h1_hdr_entry *entry; |
| 3944 | |
| 3945 | /* Be sure there is a non-empty <to> */ |
| 3946 | if (!strlen(to)) { |
| 3947 | memprintf(err, "expect <to>"); |
| 3948 | return -1; |
| 3949 | } |
| 3950 | |
| 3951 | /* Be sure only the case differs between <from> and <to> */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3952 | if (strcasecmp(from, to) != 0) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3953 | memprintf(err, "<from> and <to> must not differ execpt the case"); |
| 3954 | return -1; |
| 3955 | } |
| 3956 | |
| 3957 | /* Be sure <from> does not already existsin the tree */ |
| 3958 | if (ebis_lookup(&hdrs_map.map, from)) { |
| 3959 | memprintf(err, "duplicate entry '%s'", from); |
| 3960 | return -1; |
| 3961 | } |
| 3962 | |
| 3963 | /* Create the entry and insert it in the tree */ |
| 3964 | entry = malloc(sizeof(*entry)); |
| 3965 | if (!entry) { |
| 3966 | memprintf(err, "out of memory"); |
| 3967 | return -1; |
| 3968 | } |
| 3969 | |
| 3970 | entry->node.key = strdup(from); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 3971 | entry->name = ist(strdup(to)); |
Tim Duesterhus | 7b5777d | 2021-03-02 18:57:28 +0100 | [diff] [blame] | 3972 | if (!entry->node.key || !isttest(entry->name)) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3973 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3974 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 3975 | free(entry); |
| 3976 | memprintf(err, "out of memory"); |
| 3977 | return -1; |
| 3978 | } |
| 3979 | ebis_insert(&hdrs_map.map, &entry->node); |
| 3980 | return 0; |
| 3981 | } |
| 3982 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3983 | /* Migrate the the connection to the current thread. |
| 3984 | * Return 0 if successful, non-zero otherwise. |
| 3985 | * Expected to be called with the old thread lock held. |
| 3986 | */ |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 3987 | static int h1_takeover(struct connection *conn, int orig_tid) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3988 | { |
| 3989 | struct h1c *h1c = conn->ctx; |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 3990 | struct task *task; |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3991 | |
| 3992 | if (fd_takeover(conn->handle.fd, conn) != 0) |
| 3993 | return -1; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 3994 | |
| 3995 | if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) { |
| 3996 | /* We failed to takeover the xprt, even if the connection may |
| 3997 | * still be valid, flag it as error'd, as we have already |
| 3998 | * taken over the fd, and wake the tasklet, so that it will |
| 3999 | * destroy it. |
| 4000 | */ |
| 4001 | conn->flags |= CO_FL_ERROR; |
| 4002 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
| 4003 | return -1; |
| 4004 | } |
| 4005 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4006 | if (h1c->wait_event.events) |
| 4007 | h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 4008 | h1c->wait_event.events, &h1c->wait_event); |
| 4009 | /* To let the tasklet know it should free itself, and do nothing else, |
| 4010 | * set its context to NULL. |
| 4011 | */ |
| 4012 | h1c->wait_event.tasklet->context = NULL; |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 4013 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 4014 | |
| 4015 | task = h1c->task; |
| 4016 | if (task) { |
| 4017 | task->context = NULL; |
| 4018 | h1c->task = NULL; |
| 4019 | __ha_barrier_store(); |
| 4020 | task_kill(task); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4021 | |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4022 | h1c->task = task_new_here(); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4023 | if (!h1c->task) { |
| 4024 | h1_release(h1c); |
| 4025 | return -1; |
| 4026 | } |
| 4027 | h1c->task->process = h1_timeout_task; |
| 4028 | h1c->task->context = h1c; |
| 4029 | } |
| 4030 | h1c->wait_event.tasklet = tasklet_new(); |
| 4031 | if (!h1c->wait_event.tasklet) { |
| 4032 | h1_release(h1c); |
| 4033 | return -1; |
| 4034 | } |
| 4035 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 4036 | h1c->wait_event.tasklet->context = h1c; |
| 4037 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 4038 | SUB_RETRY_RECV, &h1c->wait_event); |
| 4039 | |
| 4040 | return 0; |
| 4041 | } |
| 4042 | |
| 4043 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4044 | static void h1_hdeaders_case_adjust_deinit() |
| 4045 | { |
| 4046 | struct ebpt_node *node, *next; |
| 4047 | struct h1_hdr_entry *entry; |
| 4048 | |
| 4049 | node = ebpt_first(&hdrs_map.map); |
| 4050 | while (node) { |
| 4051 | next = ebpt_next(node); |
| 4052 | ebpt_delete(node); |
| 4053 | entry = container_of(node, struct h1_hdr_entry, node); |
| 4054 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4055 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4056 | free(entry); |
| 4057 | node = next; |
| 4058 | } |
| 4059 | free(hdrs_map.name); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4060 | } |
| 4061 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4062 | static int cfg_h1_headers_case_adjust_postparser() |
| 4063 | { |
| 4064 | FILE *file = NULL; |
| 4065 | char *c, *key_beg, *key_end, *value_beg, *value_end; |
| 4066 | char *err; |
| 4067 | int rc, line = 0, err_code = 0; |
| 4068 | |
| 4069 | if (!hdrs_map.name) |
| 4070 | goto end; |
| 4071 | |
| 4072 | file = fopen(hdrs_map.name, "r"); |
| 4073 | if (!file) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4074 | ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4075 | hdrs_map.name); |
| 4076 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4077 | goto end; |
| 4078 | } |
| 4079 | |
| 4080 | /* now parse all lines. The file may contain only two header name per |
| 4081 | * line, separated by spaces. All heading and trailing spaces will be |
| 4082 | * ignored. Lines starting with a # are ignored. |
| 4083 | */ |
| 4084 | while (fgets(trash.area, trash.size, file) != NULL) { |
| 4085 | line++; |
| 4086 | c = trash.area; |
| 4087 | |
| 4088 | /* strip leading spaces and tabs */ |
| 4089 | while (*c == ' ' || *c == '\t') |
| 4090 | c++; |
| 4091 | |
| 4092 | /* ignore emptu lines, or lines beginning with a dash */ |
| 4093 | if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n') |
| 4094 | continue; |
| 4095 | |
| 4096 | /* look for the end of the key */ |
| 4097 | key_beg = c; |
| 4098 | while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 4099 | c++; |
| 4100 | key_end = c; |
| 4101 | |
| 4102 | /* strip middle spaces and tabs */ |
| 4103 | while (*c == ' ' || *c == '\t') |
| 4104 | c++; |
| 4105 | |
| 4106 | /* look for the end of the value, it is the end of the line */ |
| 4107 | value_beg = c; |
| 4108 | while (*c && *c != '\n' && *c != '\r') |
| 4109 | c++; |
| 4110 | value_end = c; |
| 4111 | |
| 4112 | /* trim possibly trailing spaces and tabs */ |
| 4113 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 4114 | value_end--; |
| 4115 | |
| 4116 | /* set final \0 and check entries */ |
| 4117 | *key_end = '\0'; |
| 4118 | *value_end = '\0'; |
| 4119 | |
| 4120 | err = NULL; |
| 4121 | rc = add_hdr_case_adjust(key_beg, value_beg, &err); |
| 4122 | if (rc < 0) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4123 | ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4124 | hdrs_map.name, err, line); |
| 4125 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 4126 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4127 | goto end; |
| 4128 | } |
| 4129 | if (rc > 0) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4130 | ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4131 | hdrs_map.name, err, line); |
| 4132 | err_code |= ERR_WARN; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 4133 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | |
| 4137 | end: |
| 4138 | if (file) |
| 4139 | fclose(file); |
| 4140 | hap_register_post_deinit(h1_hdeaders_case_adjust_deinit); |
| 4141 | return err_code; |
| 4142 | } |
| 4143 | |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 4144 | /* config parser for global "h1-accept-payload_=-with-any-method" */ |
| 4145 | static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx, |
| 4146 | const struct proxy *defpx, const char *file, int line, |
| 4147 | char **err) |
| 4148 | { |
| 4149 | if (too_many_args(0, args, err, NULL)) |
| 4150 | return -1; |
| 4151 | accept_payload_with_any_method = 1; |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4155 | |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4156 | /* config parser for global "h1-header-case-adjust" */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4157 | static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 4158 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4159 | char **err) |
| 4160 | { |
| 4161 | if (too_many_args(2, args, err, NULL)) |
| 4162 | return -1; |
| 4163 | if (!*(args[1]) || !*(args[2])) { |
| 4164 | memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]); |
| 4165 | return -1; |
| 4166 | } |
| 4167 | return add_hdr_case_adjust(args[1], args[2], err); |
| 4168 | } |
| 4169 | |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4170 | /* config parser for global "h1-headers-case-adjust-file" */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4171 | static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 4172 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4173 | char **err) |
| 4174 | { |
| 4175 | if (too_many_args(1, args, err, NULL)) |
| 4176 | return -1; |
| 4177 | if (!*(args[1])) { |
| 4178 | memprintf(err, "'%s' expects <file> as argument.", args[0]); |
| 4179 | return -1; |
| 4180 | } |
| 4181 | free(hdrs_map.name); |
| 4182 | hdrs_map.name = strdup(args[1]); |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4186 | /* config keyword parsers */ |
| 4187 | static struct cfg_kw_list cfg_kws = {{ }, { |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 4188 | { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method }, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4189 | { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust }, |
| 4190 | { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file }, |
| 4191 | { 0, NULL, NULL }, |
| 4192 | } |
| 4193 | }; |
| 4194 | |
| 4195 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 4196 | REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser); |
| 4197 | |
| 4198 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4199 | /****************************************/ |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 4200 | /* MUX initialization and instantiation */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4201 | /****************************************/ |
| 4202 | |
| 4203 | /* The mux operations */ |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4204 | static const struct mux_ops mux_http_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4205 | .init = h1_init, |
| 4206 | .wake = h1_wake, |
| 4207 | .attach = h1_attach, |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 4208 | .get_first_sc = h1_get_first_sc, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4209 | .detach = h1_detach, |
| 4210 | .destroy = h1_destroy, |
| 4211 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 4212 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4213 | .rcv_buf = h1_rcv_buf, |
| 4214 | .snd_buf = h1_snd_buf, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 4215 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 4216 | .rcv_pipe = h1_rcv_pipe, |
| 4217 | .snd_pipe = h1_snd_pipe, |
| 4218 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4219 | .subscribe = h1_subscribe, |
| 4220 | .unsubscribe = h1_unsubscribe, |
| 4221 | .shutr = h1_shutr, |
| 4222 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4223 | .show_fd = h1_show_fd, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 4224 | .ctl = h1_ctl, |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4225 | .takeover = h1_takeover, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 4226 | .flags = MX_FL_HTX, |
Willy Tarreau | 0a7a4fb | 2019-05-22 11:36:54 +0200 | [diff] [blame] | 4227 | .name = "H1", |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4228 | }; |
| 4229 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4230 | static const struct mux_ops mux_h1_ops = { |
| 4231 | .init = h1_init, |
| 4232 | .wake = h1_wake, |
| 4233 | .attach = h1_attach, |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 4234 | .get_first_sc = h1_get_first_sc, |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4235 | .detach = h1_detach, |
| 4236 | .destroy = h1_destroy, |
| 4237 | .avail_streams = h1_avail_streams, |
| 4238 | .used_streams = h1_used_streams, |
| 4239 | .rcv_buf = h1_rcv_buf, |
| 4240 | .snd_buf = h1_snd_buf, |
| 4241 | #if defined(USE_LINUX_SPLICE) |
| 4242 | .rcv_pipe = h1_rcv_pipe, |
| 4243 | .snd_pipe = h1_snd_pipe, |
| 4244 | #endif |
| 4245 | .subscribe = h1_subscribe, |
| 4246 | .unsubscribe = h1_unsubscribe, |
| 4247 | .shutr = h1_shutr, |
| 4248 | .shutw = h1_shutw, |
| 4249 | .show_fd = h1_show_fd, |
| 4250 | .ctl = h1_ctl, |
| 4251 | .takeover = h1_takeover, |
| 4252 | .flags = MX_FL_HTX|MX_FL_NO_UPG, |
| 4253 | .name = "H1", |
| 4254 | }; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4255 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4256 | /* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */ |
| 4257 | static struct mux_proto_list mux_proto_h1 = |
| 4258 | { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 4259 | static struct mux_proto_list mux_proto_http = |
| 4260 | { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops }; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4261 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4262 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1); |
| 4263 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http); |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4264 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4265 | /* |
| 4266 | * Local variables: |
| 4267 | * c-indent-level: 8 |
| 4268 | * c-basic-offset: 8 |
| 4269 | * End: |
| 4270 | */ |