return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED);
}
+/* Checks if the data in <buf> match the beginning of the HTTP/2 connection
+ * preface. It returns 1 if the full preface is present, 0 if it does not
+ * match at all and -1 if it can still be a partial preface, waiting for
+ * more data. The H2 preface may be received in several segments, so the
+ * connection may not be upgraded to H2 until all the preface was received.
+ */
+static int h1_match_h2_preface(const struct buffer *buf)
+{
+ const struct ist preface = ist(H2_CONN_PREFACE);
+ size_t len = b_data(buf);
+ ssize_t ret;
+
+ /* Truncate the preface to the number of available bytes to detect
+ * a partial match on a segmented preface.
+ */
+ if (len > preface.len)
+ len = preface.len;
+ ret = b_isteq(buf, 0, b_data(buf), ist2(preface.ptr, len));
+
+ return (ret == len) ? ((len < preface.len) ? -1 : 1) : 0;
+}
+
/* callback called on any event by the connection handler.
* It applies changes and returns zero, -1 if everything was destroyed,
* and -2 if the mux was destroyed, but the connection is still alive because
struct h1s *h1s = h1c->h1s;
struct buffer *buf;
size_t count;
+ int is_upgrade;
/* When it happens for a backend connection, we may release it (it is probably a 408) */
if (h1c->flags & H1C_F_IS_BACK)
/* First of all handle H2 preface (except for DRAINING mode).
* If H1 to H2 upgrade is allowed, no need to create the H1
* stream. If not allowed, a 405 must be returned.
+ *
+ * The preface can be received in several segments. If so, it will
+ * be detected as a partial preface and we must wait for more data.
+ * Otherwise, with the full preface, <is_upgrade> is > 0 and the
+ * H1 stream must not be created.
*/
- if (h1c->state != H1_CS_DRAINING &&
- b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) {
+ is_upgrade = (h1c->state != H1_CS_DRAINING) ?
+ h1_match_h2_preface(&h1c->ibuf) : 0;
+
+ if (is_upgrade > 0) {
if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request (the flag is */
!(h1s && (h1s->flags & H1S_F_NOT_FIRST)) && /* moved to the h1s once created) */
!(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */
}
}
+ if (is_upgrade < 0) {
+ /* Partial H2 preface: it may be received in several
+ * segments, so wait for more data to complete the
+ * preface instead of parsing the buffered data as an
+ * H1 request.
+ */
+ TRACE_STATE("partial H2 preface, waiting for more data", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn);
+ h1_set_idle_expiration(h1c);
+ h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event);
+ goto no_parsing;
+ }
+
/* Create the H1 stream if not already there */
if (!h1s) {
h1s = h1c_frt_stream_new(h1c, NULL, sess);