diff options
| author | Xe Iaso <me@christine.website> | 2022-03-21 20:14:14 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-21 20:14:14 -0400 |
| commit | 8b747c1c40876c6668191594eddcb260199cdb7f (patch) | |
| tree | 86edb9bc382751c6a32f5f2946ff235ea06674ba /src/domainsocket.rs | |
| parent | f45ca40ae1052d46611ff2f27ad281695afc4f8f (diff) | |
| download | xesite-8b747c1c40876c6668191594eddcb260199cdb7f.tar.xz xesite-8b747c1c40876c6668191594eddcb260199cdb7f.zip | |
Rewrite the site routing with Axum (#441)
* broken state
Signed-off-by: Xe Iaso <me@christine.website>
* fix???
Signed-off-by: Xe Iaso <me@christine.website>
* Port everything else to axum
Signed-off-by: Xe <me@christine.website>
* headers
Signed-off-by: Xe Iaso <me@christine.website>
* site update post
Signed-off-by: Christine Dodrill <me@christine.website>
* fix headers
Signed-off-by: Xe Iaso <me@christine.website>
* remove warp example
Signed-off-by: Xe Iaso <me@christine.website>
* 80c wrap
Signed-off-by: Xe Iaso <me@christine.website>
* bump version
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'src/domainsocket.rs')
| -rw-r--r-- | src/domainsocket.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/domainsocket.rs b/src/domainsocket.rs new file mode 100644 index 0000000..ef731f5 --- /dev/null +++ b/src/domainsocket.rs @@ -0,0 +1,94 @@ +use axum::extract::connect_info; +use futures::ready; +use hyper::{ + client::connect::{Connected, Connection}, + server::accept::Accept, +}; +use std::{ + io, + pin::Pin, + sync::Arc, + task::{Context, Poll}, +}; +use tokio::{ + io::{AsyncRead, AsyncWrite}, + net::{unix::UCred, UnixListener, UnixStream}, +}; +use tower::BoxError; + +pub struct ServerAccept { + pub uds: UnixListener, +} + +impl Accept for ServerAccept { + type Conn = UnixStream; + type Error = BoxError; + + fn poll_accept( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Conn, Self::Error>>> { + let (stream, _addr) = ready!(self.uds.poll_accept(cx))?; + Poll::Ready(Some(Ok(stream))) + } +} + +pub struct ClientConnection { + pub stream: UnixStream, +} + +impl AsyncWrite for ClientConnection { + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll<Result<usize, io::Error>> { + Pin::new(&mut self.stream).poll_write(cx, buf) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { + Pin::new(&mut self.stream).poll_flush(cx) + } + + fn poll_shutdown( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<(), io::Error>> { + Pin::new(&mut self.stream).poll_shutdown(cx) + } +} + +impl AsyncRead for ClientConnection { + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + buf: &mut tokio::io::ReadBuf<'_>, + ) -> Poll<io::Result<()>> { + Pin::new(&mut self.stream).poll_read(cx, buf) + } +} + +impl Connection for ClientConnection { + fn connected(&self) -> Connected { + Connected::new() + } +} + +#[derive(Clone, Debug)] +#[allow(dead_code)] +pub struct UdsConnectInfo { + pub peer_addr: Arc<tokio::net::unix::SocketAddr>, + pub peer_cred: UCred, +} + +impl connect_info::Connected<&UnixStream> for UdsConnectInfo { + fn connect_info(target: &UnixStream) -> Self { + let peer_addr = target.peer_addr().unwrap(); + let peer_cred = target.peer_cred().unwrap(); + + Self { + peer_addr: Arc::new(peer_addr), + peer_cred, + } + } +} |
