Made the send functions methods of Connection struct
This commit is contained in:
@@ -9,9 +9,8 @@ use url::Url;
|
|||||||
use crate::config;
|
use crate::config;
|
||||||
use crate::status;
|
use crate::status;
|
||||||
use crate::util;
|
use crate::util;
|
||||||
use crate::Connection;
|
|
||||||
|
|
||||||
pub async fn cgi(con: Connection, path: PathBuf, url: Url) -> Result<(), io::Error> {
|
pub async fn cgi(mut con: util::Connection, path: PathBuf, url: Url) -> Result<(), io::Error> {
|
||||||
let mut envs = HashMap::new();
|
let mut envs = HashMap::new();
|
||||||
envs.insert("GEMINI_URL", url.as_str());
|
envs.insert("GEMINI_URL", url.as_str());
|
||||||
envs.insert("SERVER_NAME", url.host_str().unwrap());
|
envs.insert("SERVER_NAME", url.host_str().unwrap());
|
||||||
@@ -33,12 +32,12 @@ pub async fn cgi(con: Connection, path: PathBuf, url: Url) -> Result<(), io::Err
|
|||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
util::send_status(con.stream, status::Status::CGIError, "CGI Error!").await?;
|
con.send_status(status::Status::CGIError, "CGI Error!").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let cmd = String::from_utf8(cmd.stdout).unwrap();
|
let cmd = String::from_utf8(cmd.stdout).unwrap();
|
||||||
// if cmd.starts_with("20") {
|
// if cmd.starts_with("20") {
|
||||||
util::send_raw(con.stream, cmd).await?;
|
con.send_raw(cmd).await?;
|
||||||
//util::send_body(stream, status::Status::Success, "text/gemini", Some(cmd)).await?;
|
//util::send_body(stream, status::Status::Success, "text/gemini", Some(cmd)).await?;
|
||||||
// }
|
// }
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
27
src/main.rs
27
src/main.rs
@@ -33,14 +33,6 @@ mod status;
|
|||||||
mod tls;
|
mod tls;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub struct Connection {
|
|
||||||
stream: TlsStream<TcpStream>,
|
|
||||||
peer_addr: SocketAddr,
|
|
||||||
hostname: String,
|
|
||||||
dir: String,
|
|
||||||
cgi: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
||||||
let meta = fs::metadata(&path).expect("Unable to read metadata");
|
let meta = fs::metadata(&path).expect("Unable to read metadata");
|
||||||
if meta.is_file() {
|
if meta.is_file() {
|
||||||
@@ -60,7 +52,7 @@ fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
|||||||
return Ok(list);
|
return Ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
async fn handle_connection(mut con: util::Connection) -> Result<(), io::Error> {
|
||||||
let now: DateTime<Utc> = Utc::now();
|
let now: DateTime<Utc> = Utc::now();
|
||||||
println!("{} New Connection: {}", now, con.peer_addr);
|
println!("{} New Connection: {}", now, con.peer_addr);
|
||||||
let mut buffer = [0; 512];
|
let mut buffer = [0; 512];
|
||||||
@@ -71,13 +63,12 @@ async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
|||||||
let url = Url::parse(&request).unwrap();
|
let url = Url::parse(&request).unwrap();
|
||||||
|
|
||||||
if Some(con.hostname.as_str()) != url.host_str() {
|
if Some(con.hostname.as_str()) != url.host_str() {
|
||||||
util::send_status(con.stream, status::Status::PermanentFailure, "Url doesn't match certificate!").await?;
|
con.send_status(status::Status::PermanentFailure, "Url doesn't match certificate!").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
util::send_status(
|
con.send_status(
|
||||||
con.stream,
|
|
||||||
status::Status::ProxyRequestRefused,
|
status::Status::ProxyRequestRefused,
|
||||||
"Not a gemini scheme!\r\n",
|
"Not a gemini scheme!\r\n",
|
||||||
)
|
)
|
||||||
@@ -86,7 +77,7 @@ async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if url.path().to_string().contains("..") {
|
if url.path().to_string().contains("..") {
|
||||||
util::send_status(con.stream, status::Status::PermanentFailure, "Not in path!").await?;
|
con.send_status(status::Status::PermanentFailure, "Not in path!").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +87,7 @@ async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
util::send_status(con.stream, status::Status::NotFound, "Not found!\r\n").await?;
|
con.send_status(status::Status::NotFound, "Not found!\r\n").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,8 +96,7 @@ async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
|||||||
|
|
||||||
if meta.is_dir() {
|
if meta.is_dir() {
|
||||||
if !url.path().ends_with("/") {
|
if !url.path().ends_with("/") {
|
||||||
util::send_status(
|
con.send_status(
|
||||||
con.stream,
|
|
||||||
status::Status::RedirectPermanent,
|
status::Status::RedirectPermanent,
|
||||||
format!("{}/\r\n", url).as_str(),
|
format!("{}/\r\n", url).as_str(),
|
||||||
)
|
)
|
||||||
@@ -125,8 +115,7 @@ async fn handle_connection(mut con: Connection) -> Result<(), io::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let content = get_content(path, url)?;
|
let content = get_content(path, url)?;
|
||||||
util::send_body(
|
con.send_body(
|
||||||
con.stream,
|
|
||||||
status::Status::Success,
|
status::Status::Success,
|
||||||
"text/gemini",
|
"text/gemini",
|
||||||
Some(content),
|
Some(content),
|
||||||
@@ -179,7 +168,7 @@ fn main() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let con = Connection {
|
let con = util::Connection {
|
||||||
stream,
|
stream,
|
||||||
peer_addr,
|
peer_addr,
|
||||||
hostname,
|
hostname,
|
||||||
|
|||||||
29
src/util.rs
29
src/util.rs
@@ -1,37 +1,46 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
use std::net::SocketAddr;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
use tokio_rustls::server::TlsStream;
|
use tokio_rustls::server::TlsStream;
|
||||||
|
|
||||||
use crate::status;
|
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(
|
pub async fn send_status(
|
||||||
stream: TlsStream<TcpStream>,
|
&mut self,
|
||||||
stat: status::Status,
|
stat: status::Status,
|
||||||
meta: &str,
|
meta: &str,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
send_body(stream, stat, meta, None).await?;
|
self.send_body(stat, meta, None).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_body(
|
pub async fn send_body(
|
||||||
mut stream: TlsStream<TcpStream>,
|
&mut self,
|
||||||
stat: status::Status,
|
stat: status::Status,
|
||||||
meta: &str,
|
meta: &str,
|
||||||
body: Option<String>,
|
body: Option<String>,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
let mut s = format!("{}\t{}\r\n", stat as u8, meta);
|
let mut s = format!("{}\t{}\r\n", stat as u8, meta);
|
||||||
stream.write_all(s.as_bytes()).await?;
|
|
||||||
stream.flush().await?;
|
|
||||||
if let Some(b) = body {
|
if let Some(b) = body {
|
||||||
s = format!("{}", b);
|
s += &b;
|
||||||
}
|
}
|
||||||
send_raw(stream, s).await?;
|
self.send_raw(s).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_raw(mut stream: TlsStream<TcpStream>, body: String) -> Result<(), io::Error> {
|
pub async fn send_raw(&mut self, body: String) -> Result<(), io::Error> {
|
||||||
stream.write_all(body.as_bytes()).await?;
|
self.stream.write_all(body.as_bytes()).await?;
|
||||||
stream.flush().await?;
|
self.stream.flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user