diff --git a/src/common/upgrade.rs b/src/common/upgrade.rs index 8002b68b..06836619 100644 --- a/src/common/upgrade.rs +++ b/src/common/upgrade.rs @@ -52,4 +52,40 @@ impl Upgrade { pub fn websocket() -> Upgrade { Upgrade(HeaderValue::from_static("websocket")) } + + /// Constructs an `Upgrade: h2c` header, for HTTP/2 over TCP. There's no + /// constructor for HTTP/2 over TLS, as using ALPN or prior knowledge is + /// better-suited to that usecase. + pub fn h2c() -> Upgrade { + Upgrade(HeaderValue::from_static("h2c")) + } + + /// Constructs an `Upgrade` header with the given `HeaderValue` + pub fn new(value: HeaderValue) -> Upgrade { + Upgrade(value) + } + + /// Returns the header value, e.g. "websocket" or "h2c", or a + /// comma-separated list, see RFC 7230: + /// + /// + /// If the header value cannot be represented as a utf-8 string, + /// `None` is returned. + pub fn to_str(&self) -> Option<&str> { + self.0.to_str().ok() + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::*; + + #[test] + fn websocket() { + let s = "websocket"; + let loc = test_decode::(&[s]).unwrap(); + + assert_eq!(loc.to_str(), Some(s)); + } }