use crate::{Headers, HttpContext, Method, Transport, TypeSet, Version};
use futures_lite::{AsyncRead, AsyncWrite};
use std::{net::IpAddr, sync::Arc};
use trillium_http::Swansong;
use trillium_macros::{AsyncRead, AsyncWrite};
#[derive(Debug, AsyncWrite, AsyncRead)]
pub struct Upgrade(trillium_http::Upgrade<Box<dyn Transport>>);
impl<T: Transport + 'static> From<trillium_http::Upgrade<T>> for Upgrade {
fn from(value: trillium_http::Upgrade<T>) -> Self {
Self(value.map_transport(|t| Box::new(t) as Box<dyn Transport>))
}
}
impl<T: Transport + 'static> From<trillium_http::Conn<T>> for Upgrade {
fn from(value: trillium_http::Conn<T>) -> Self {
trillium_http::Upgrade::from(value).into()
}
}
impl From<crate::Conn> for Upgrade {
fn from(value: crate::Conn) -> Self {
Self(value.inner.into())
}
}
impl AsRef<trillium_http::Upgrade<Box<dyn Transport>>> for Upgrade {
fn as_ref(&self) -> &trillium_http::Upgrade<Box<dyn Transport>> {
&self.0
}
}
impl AsMut<trillium_http::Upgrade<Box<dyn Transport>>> for Upgrade {
fn as_mut(&mut self) -> &mut trillium_http::Upgrade<Box<dyn Transport>> {
&mut self.0
}
}
impl Upgrade {
pub fn request_headers(&self) -> &Headers {
self.0.request_headers()
}
pub fn take_request_headers(&mut self) -> Headers {
std::mem::take(self.0.request_headers_mut())
}
pub fn method(&self) -> Method {
self.0.method()
}
pub fn state(&self) -> &TypeSet {
self.0.state()
}
pub fn take_state(&mut self) -> TypeSet {
std::mem::take(self.0.state_mut())
}
pub fn state_mut(&mut self) -> &mut TypeSet {
self.0.state_mut()
}
pub fn transport(&self) -> &dyn Transport {
self.0.transport().as_ref()
}
pub fn transport_mut(&mut self) -> (&[u8], &mut dyn Transport) {
let (buffer, transport) = self.0.buffer_and_transport_mut();
(&*buffer, &mut **transport)
}
pub fn into_transport(mut self) -> (Vec<u8>, Box<dyn Transport>) {
let buffer = self.0.take_buffer();
(buffer, self.0.into_transport())
}
pub fn peer_ip(&self) -> Option<IpAddr> {
self.0.peer_ip()
}
pub fn authority(&self) -> Option<&str> {
self.0.authority()
}
pub fn scheme(&self) -> Option<&str> {
self.0.scheme()
}
pub fn protocol(&self) -> Option<&str> {
self.0.protocol()
}
pub fn http_version(&self) -> &Version {
self.0.http_version()
}
pub fn is_secure(&self) -> bool {
self.0.is_secure()
}
pub fn shared_state(&self) -> &TypeSet {
self.0.shared_state()
}
pub fn path(&self) -> &str {
self.0.path()
}
pub fn querystring(&self) -> &str {
self.0.querystring()
}
pub fn swansong(&self) -> Swansong {
self.0.context().swansong().clone()
}
pub fn context(&self) -> Arc<HttpContext> {
self.0.context().clone()
}
pub fn h3_connection(&self) -> Option<Arc<trillium_http::h3::H3Connection>> {
self.0.h3_connection().cloned()
}
}