1use crate::http::RequestContext;
6use crate::{Protocol, address::Authority};
7use rama_core::{Context, error::OpaqueError};
8use rama_http_types::{Request, Version, dep::http::request::Parts as HttpParts};
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct TransportContext {
14 pub protocol: TransportProtocol,
16
17 pub app_protocol: Option<Protocol>,
19
20 pub http_version: Option<Version>,
22
23 pub authority: Authority,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
30pub enum TransportProtocol {
32 Tcp,
34 Udp,
36}
37
38pub trait TryRefIntoTransportContext<State> {
44 type Error;
46
47 fn try_ref_into_transport_ctx(
49 &self,
50 ctx: &Context<State>,
51 ) -> Result<TransportContext, Self::Error>;
52}
53
54impl<State, Body> TryFrom<(&Context<State>, &Request<Body>)> for TransportContext {
55 type Error = OpaqueError;
56
57 fn try_from(
58 (ctx, req): (&Context<State>, &Request<Body>),
59 ) -> Result<TransportContext, Self::Error> {
60 Ok(match ctx.get::<RequestContext>() {
61 Some(req_ctx) => req_ctx.into(),
62 None => {
63 let req_ctx = RequestContext::try_from((ctx, req))?;
64 req_ctx.into()
65 }
66 })
67 }
68}
69
70impl<State> TryFrom<(&Context<State>, &HttpParts)> for TransportContext {
71 type Error = OpaqueError;
72
73 fn try_from(
74 (ctx, parts): (&Context<State>, &HttpParts),
75 ) -> Result<TransportContext, Self::Error> {
76 Ok(match ctx.get::<RequestContext>() {
77 Some(req_ctx) => req_ctx.into(),
78 None => {
79 let req_ctx = RequestContext::try_from((ctx, parts))?;
80 req_ctx.into()
81 }
82 })
83 }
84}