Added to_str for Status
This commit is contained in:
@@ -33,7 +33,7 @@ pub async fn cgi(mut con: conn::Connection, path: PathBuf, url: Url) -> Result<(
|
|||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
con.send_status(status::Status::CGIError, "CGI Error!").await?;
|
con.send_status(status::Status::CGIError, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let cmd = String::from_utf8(cmd.stdout).unwrap();
|
let cmd = String::from_utf8(cmd.stdout).unwrap();
|
||||||
|
|||||||
14
src/conn.rs
14
src/conn.rs
@@ -4,7 +4,7 @@ use tokio::net::TcpStream;
|
|||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
use tokio_openssl::SslStream;
|
use tokio_openssl::SslStream;
|
||||||
|
|
||||||
use crate::status;
|
use crate::status::Status;
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub stream: SslStream<TcpStream>,
|
pub stream: SslStream<TcpStream>,
|
||||||
@@ -14,8 +14,8 @@ pub struct Connection {
|
|||||||
impl Connection {
|
impl Connection {
|
||||||
pub async fn send_status(
|
pub async fn send_status(
|
||||||
&mut self,
|
&mut self,
|
||||||
stat: status::Status,
|
stat: Status,
|
||||||
meta: &str,
|
meta: Option<&str>,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
self.send_body(stat, meta, None).await?;
|
self.send_body(stat, meta, None).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -23,10 +23,14 @@ pub async fn send_status(
|
|||||||
|
|
||||||
pub async fn send_body(
|
pub async fn send_body(
|
||||||
&mut self,
|
&mut self,
|
||||||
stat: status::Status,
|
stat: Status,
|
||||||
meta: &str,
|
meta: Option<&str>,
|
||||||
body: Option<String>,
|
body: Option<String>,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
|
let meta = match meta {
|
||||||
|
Some(m) => m,
|
||||||
|
None => &stat.to_str(),
|
||||||
|
};
|
||||||
let mut s = format!("{}\t{}\r\n", stat as u8, meta);
|
let mut s = format!("{}\t{}\r\n", stat as u8, meta);
|
||||||
if let Some(b) = body {
|
if let Some(b) = body {
|
||||||
s += &b;
|
s += &b;
|
||||||
|
|||||||
22
src/main.rs
22
src/main.rs
@@ -54,7 +54,7 @@ fn get_mime(path: &PathBuf) -> String {
|
|||||||
async fn get_binary(mut con: conn::Connection, path:PathBuf, meta: String) -> io::Result<()> {
|
async fn get_binary(mut con: conn::Connection, path:PathBuf, meta: String) -> io::Result<()> {
|
||||||
let fd = File::open(path)?;
|
let fd = File::open(path)?;
|
||||||
let mut reader = BufReader::with_capacity(1024*1024,fd);
|
let mut reader = BufReader::with_capacity(1024*1024,fd);
|
||||||
con.send_status(status::Status::Success, &meta).await?;
|
con.send_status(status::Status::Success, Some(&meta)).await?;
|
||||||
loop {
|
loop {
|
||||||
let len = {
|
let len = {
|
||||||
let buf = reader.fill_buf()?;
|
let buf = reader.fill_buf()?;
|
||||||
@@ -107,7 +107,7 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
Ok(request) => request,
|
Ok(request) => request,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
println!("Bad Request");
|
println!("Bad Request");
|
||||||
con.send_status(status::Status::BadRequest, "Bad Request!").await?;
|
con.send_status(status::Status::BadRequest, None).await?;
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -118,19 +118,19 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
|
|
||||||
let url = match Url::parse(&request) {
|
let url = match Url::parse(&request) {
|
||||||
Ok(url) => url,
|
Ok(url) => url,
|
||||||
Err(_) => { con.send_status(status::Status::BadRequest, "Bad Request!").await?;
|
Err(_) => { con.send_status(status::Status::BadRequest, None).await?;
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if Some(srv.hostname.as_str()) != url.host_str() {
|
if Some(srv.hostname.as_str()) != url.host_str() {
|
||||||
con.send_status(status::Status::ProxyRequestRefused, "Url doesn't match certificate!").await?;
|
con.send_status(status::Status::ProxyRequestRefused, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
match url.port() {
|
match url.port() {
|
||||||
Some(p) => { if p != srv.port {
|
Some(p) => { if p != srv.port {
|
||||||
con.send_status(status::Status::ProxyRequestRefused, "Wrong Port!").await?;
|
con.send_status(status::Status::ProxyRequestRefused, None).await?;
|
||||||
}},
|
}},
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
@@ -138,7 +138,7 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
con.send_status(
|
con.send_status(
|
||||||
status::Status::ProxyRequestRefused,
|
status::Status::ProxyRequestRefused,
|
||||||
"Not a gemini scheme!",
|
None,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -179,7 +179,7 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
con.send_status(status::Status::NotFound, "Not found!").await?;
|
con.send_status(status::Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +193,7 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
println!("{}", url);
|
println!("{}", url);
|
||||||
con.send_status(
|
con.send_status(
|
||||||
status::Status::RedirectPermanent,
|
status::Status::RedirectPermanent,
|
||||||
format!("{}/", url).as_str(),
|
Some(format!("{}/", url).as_str()),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -219,14 +219,14 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
con.send_status(
|
con.send_status(
|
||||||
status::Status::CGIError, "CGI Error!").await?;
|
status::Status::CGIError, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if perm.mode() & 0o0444 != 0o0444 {
|
if perm.mode() & 0o0444 != 0o0444 {
|
||||||
con.send_status(
|
con.send_status(
|
||||||
status::Status::NotFound, "Not Found!").await?;
|
status::Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
|
|||||||
let content = get_content(path, url)?;
|
let content = get_content(path, url)?;
|
||||||
con.send_body(
|
con.send_body(
|
||||||
status::Status::Success,
|
status::Status::Success,
|
||||||
mime.as_str(),
|
Some(mime.as_str()),
|
||||||
Some(content),
|
Some(content),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ use crate::status;
|
|||||||
pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Result<(), io::Error> {
|
pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Result<(), io::Error> {
|
||||||
let p: Vec<&str> = u.path().trim_start_matches("/").splitn(2, "/").collect();
|
let p: Vec<&str> = u.path().trim_start_matches("/").splitn(2, "/").collect();
|
||||||
if p.len() == 1 {
|
if p.len() == 1 {
|
||||||
con.send_status(status::Status::NotFound, "Not Found").await?;
|
con.send_status(status::Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if p[1] == "" || p[1] == "/" {
|
if p[1] == "" || p[1] == "/" {
|
||||||
con.send_status(status::Status::NotFound, "Not Found").await?;
|
con.send_status(status::Status::NotFound, None).await?;
|
||||||
return Ok(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
let addr = addr
|
let addr = addr
|
||||||
@@ -36,7 +36,7 @@ pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Resu
|
|||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Error connecting to proxy");
|
eprintln!("Error connecting to proxy");
|
||||||
con.send_status(status::Status::ProxyError, "Error Connecting to proxy").await?;
|
con.send_status(status::Status::ProxyError, None).await?;
|
||||||
return Ok(())
|
return Ok(())
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -44,7 +44,7 @@ pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Resu
|
|||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Error connecting to proxy");
|
eprintln!("Error connecting to proxy");
|
||||||
con.send_status(status::Status::ProxyError, "Error Connecting to proxy").await?;
|
con.send_status(status::Status::ProxyError, None).await?;
|
||||||
return Ok(())
|
return Ok(())
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub enum Status {
|
pub enum Status {
|
||||||
Input = 10,
|
Input = 10,
|
||||||
Success = 20,
|
Success = 20,
|
||||||
@@ -26,6 +26,35 @@ pub enum Status {
|
|||||||
ExpiredCertificateRejected = 65,
|
ExpiredCertificateRejected = 65,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Status {
|
||||||
|
pub fn to_str(&self) -> &str {
|
||||||
|
let meta = match self {
|
||||||
|
Status::Input => "Input",
|
||||||
|
Status::Success => "Success",
|
||||||
|
Status::SuccessEndOfSession => "Success End Of Session",
|
||||||
|
Status::RedirectTemporary => "Redirect Temporary",
|
||||||
|
Status::RedirectPermanent => "Redirect Permanent",
|
||||||
|
Status::TemporaryFailure => "Temporary Failure",
|
||||||
|
Status::ServerUnavailable => "Server Unavailable",
|
||||||
|
Status::CGIError => "CGI Error!",
|
||||||
|
Status::ProxyError => "Proxy Error!",
|
||||||
|
Status::SlowDown => "Slow Down!",
|
||||||
|
Status::PermanentFailure => "Permanent Failure",
|
||||||
|
Status::NotFound => "Not Found!",
|
||||||
|
Status::Gone => "Gone!",
|
||||||
|
Status::ProxyRequestRefused => "Proxy Requet Refused",
|
||||||
|
Status::BadRequest => "Bad Request!",
|
||||||
|
Status::ClientCertificateRequired => "Client Certificate Required",
|
||||||
|
Status::TransientCertificateRequested => "Transient Certificate Requested",
|
||||||
|
Status::AuthorisedCertificateRequired => "Authorised Certificate Required",
|
||||||
|
Status::CertificateNotAccepted => "Certificate Not Accepted",
|
||||||
|
Status::FutureCertificateRejected => "Future Certificate Rejected",
|
||||||
|
Status::ExpiredCertificateRejected => "Expired Certificate Rejected",
|
||||||
|
};
|
||||||
|
return meta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Status {
|
impl fmt::Display for Status {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{:?}", self)
|
write!(f, "{:?}", self)
|
||||||
|
|||||||
Reference in New Issue
Block a user