Skip to main content

is_h2_preface

Function is_h2_preface 

Source
pub fn is_h2_preface(input: &[u8]) -> Option<bool>
Expand description

Whether input starts with the HTTP/2 connection preface: Some(true) when the first 24 bytes are the preface, Some(false) as soon as a byte differs from it, and None while input is a shorter, so far matching prefix (read more to decide). At most 24 compares.

ServerConn checks this at the start of every connection and refuses the preface with HttpError::Http2Preface; a driver that accepts prior-knowledge HTTP/2 (behind a trusted proxy only) checks it first and routes the connection to an HTTP/2 engine.

ยงExamples

use http_net::is_h2_preface;

assert_eq!(is_h2_preface(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n\x00\x00"), Some(true));
assert_eq!(is_h2_preface(b"PRI * HTTP/2"), None);
assert_eq!(is_h2_preface(b"GET / HTTP/1.1\r\n"), Some(false));
assert_eq!(is_h2_preface(b""), None);