Skip to main content

rama_tls/
http.rs

1//! HTTP ⇄ TLS glue.
2//!
3//! `ApplicationProtocol` (ALPN) ⇄ http `Version` conversions, which need both
4//! the TLS enum vocabulary (this crate) and the http `Version` (`rama-net`).
5
6use crate::ApplicationProtocol;
7use rama_core::error::{BoxError, BoxErrorExt as _, ErrorExt as _};
8use rama_net::http::Version;
9
10impl TryFrom<Version> for ApplicationProtocol {
11    type Error = BoxError;
12
13    fn try_from(value: Version) -> Result<Self, Self::Error> {
14        Ok(match value {
15            Version::HTTP_09 => Self::HTTP_09,
16            Version::HTTP_10 => Self::HTTP_10,
17            Version::HTTP_11 => Self::HTTP_11,
18            Version::HTTP_2 => Self::HTTP_2,
19            Version::HTTP_3 => Self::HTTP_3,
20        })
21    }
22}
23
24impl TryFrom<ApplicationProtocol> for Version {
25    type Error = BoxError;
26
27    fn try_from(value: ApplicationProtocol) -> Result<Self, Self::Error> {
28        (&value).try_into()
29    }
30}
31
32impl TryFrom<&ApplicationProtocol> for Version {
33    type Error = BoxError;
34
35    fn try_from(value: &ApplicationProtocol) -> Result<Self, Self::Error> {
36        Ok(match value {
37            ApplicationProtocol::HTTP_09 => Self::HTTP_09,
38            ApplicationProtocol::HTTP_10 => Self::HTTP_10,
39            ApplicationProtocol::HTTP_11 => Self::HTTP_11,
40            ApplicationProtocol::HTTP_2 => Self::HTTP_2,
41            ApplicationProtocol::HTTP_3 => Self::HTTP_3,
42            alpn => {
43                return Err(
44                    BoxError::from_static_str("cannot convert given alpn to http version")
45                        .context_field("alpn", alpn.clone()),
46                );
47            }
48        })
49    }
50}