1use crate::{Headers, HttpContext, Method, Transport, TypeSet, Version};
2use futures_lite::{AsyncRead, AsyncWrite};
3use std::{net::IpAddr, sync::Arc};
4use trillium_http::Swansong;
5use trillium_macros::{AsyncRead, AsyncWrite};
6
7#[derive(Debug, AsyncWrite, AsyncRead)]
9pub struct Upgrade(trillium_http::Upgrade<Box<dyn Transport>>);
10
11impl<T: Transport + 'static> From<trillium_http::Upgrade<T>> for Upgrade {
12 fn from(value: trillium_http::Upgrade<T>) -> Self {
13 Self(value.map_transport(|t| Box::new(t) as Box<dyn Transport>))
14 }
15}
16
17impl<T: Transport + 'static> From<trillium_http::Conn<T>> for Upgrade {
18 fn from(value: trillium_http::Conn<T>) -> Self {
19 trillium_http::Upgrade::from(value).into()
20 }
21}
22
23impl From<crate::Conn> for Upgrade {
24 fn from(value: crate::Conn) -> Self {
25 Self(value.inner.into())
26 }
27}
28
29impl AsRef<trillium_http::Upgrade<Box<dyn Transport>>> for Upgrade {
30 fn as_ref(&self) -> &trillium_http::Upgrade<Box<dyn Transport>> {
31 &self.0
32 }
33}
34
35impl AsMut<trillium_http::Upgrade<Box<dyn Transport>>> for Upgrade {
36 fn as_mut(&mut self) -> &mut trillium_http::Upgrade<Box<dyn Transport>> {
37 &mut self.0
38 }
39}
40
41impl Upgrade {
42 pub fn request_headers(&self) -> &Headers {
44 self.0.request_headers()
45 }
46
47 pub fn take_request_headers(&mut self) -> Headers {
49 std::mem::take(self.0.request_headers_mut())
50 }
51
52 pub fn method(&self) -> Method {
54 self.0.method()
55 }
56
57 pub fn state(&self) -> &TypeSet {
59 self.0.state()
60 }
61
62 pub fn take_state(&mut self) -> TypeSet {
64 std::mem::take(self.0.state_mut())
65 }
66
67 pub fn state_mut(&mut self) -> &mut TypeSet {
69 self.0.state_mut()
70 }
71
72 pub fn transport(&self) -> &dyn Transport {
74 self.0.transport().as_ref()
75 }
76
77 pub fn transport_mut(&mut self) -> (&[u8], &mut dyn Transport) {
82 let (buffer, transport) = self.0.buffer_and_transport_mut();
83 (&*buffer, &mut **transport)
84 }
85
86 pub fn into_transport(mut self) -> (Vec<u8>, Box<dyn Transport>) {
91 let buffer = self.0.take_buffer();
92 (buffer, self.0.into_transport())
93 }
94
95 pub fn peer_ip(&self) -> Option<IpAddr> {
97 self.0.peer_ip()
98 }
99
100 pub fn authority(&self) -> Option<&str> {
102 self.0.authority()
103 }
104
105 pub fn scheme(&self) -> Option<&str> {
107 self.0.scheme()
108 }
109
110 pub fn protocol(&self) -> Option<&str> {
112 self.0.protocol()
113 }
114
115 pub fn http_version(&self) -> &Version {
117 self.0.http_version()
118 }
119
120 pub fn is_secure(&self) -> bool {
122 self.0.is_secure()
123 }
124
125 pub fn shared_state(&self) -> &TypeSet {
127 self.0.shared_state()
128 }
129
130 pub fn path(&self) -> &str {
132 self.0.path()
133 }
134
135 pub fn querystring(&self) -> &str {
137 self.0.querystring()
138 }
139
140 pub fn swansong(&self) -> Swansong {
142 self.0.context().swansong().clone()
143 }
144
145 pub fn context(&self) -> Arc<HttpContext> {
147 self.0.context().clone()
148 }
149
150 pub fn h3_connection(&self) -> Option<Arc<trillium_http::h3::H3Connection>> {
152 self.0.h3_connection().cloned()
153 }
154}