use std::{
fmt, io,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use scion_quic::h3::client::{H3DuplexStream, Http3Client};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use crate::authority::Authority;
pub(crate) fn connect_request(authority: &Authority) -> http::Request<()> {
http::Request::builder()
.method(http::Method::CONNECT)
.uri(format!("https://{authority}"))
.body(())
.expect("a valid authority forms a valid URI")
}
pub struct Tunnel {
stream: H3DuplexStream,
_connection: Arc<Http3Client>,
}
impl Tunnel {
pub(crate) fn new(stream: H3DuplexStream, connection: Arc<Http3Client>) -> Self {
Tunnel {
stream,
_connection: connection,
}
}
}
impl AsyncRead for Tunnel {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().stream).poll_read(cx, buf)
}
}
impl AsyncWrite for Tunnel {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().stream).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().stream).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().stream).poll_shutdown(cx)
}
}
impl fmt::Debug for Tunnel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Tunnel").finish_non_exhaustive()
}
}