Changed util to conn

This commit is contained in:
int 80h
2020-04-28 16:21:31 -04:00
parent f0e6056033
commit 596783d613
4 changed files with 14 additions and 9 deletions

46
src/conn.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::io;
use std::net::SocketAddr;
use tokio::net::TcpStream;
use tokio::prelude::*;
use tokio_rustls::server::TlsStream;
use crate::status;
pub struct Connection {
pub stream: TlsStream<TcpStream>,
pub peer_addr: SocketAddr,
pub hostname: String,
pub dir: String,
pub cgi: String,
}
impl Connection {
pub async fn send_status(
&mut self,
stat: status::Status,
meta: &str,
) -> Result<(), io::Error> {
self.send_body(stat, meta, None).await?;
Ok(())
}
pub async fn send_body(
&mut self,
stat: status::Status,
meta: &str,
body: Option<String>,
) -> Result<(), io::Error> {
let mut s = format!("{}\t{}\r\n", stat as u8, meta);
if let Some(b) = body {
s += &b;
}
self.send_raw(s.as_bytes()).await?;
Ok(())
}
pub async fn send_raw(&mut self, body: &[u8]) -> Result<(), io::Error> {
self.stream.write_all(body).await?;
self.stream.flush().await?;
Ok(())
}
}