mirror of
https://github.com/YGGverse/nexy.git
synced 2026-03-31 17:25:27 +00:00
apply minor optimizations
This commit is contained in:
parent
9a40d2a7a5
commit
90b8b8c921
3 changed files with 44 additions and 37 deletions
|
|
@ -3,7 +3,7 @@ pub enum Response<'a> {
|
||||||
/// Includes reference to the original request
|
/// Includes reference to the original request
|
||||||
AccessDenied(&'a str),
|
AccessDenied(&'a str),
|
||||||
/// Includes query + server-side error description
|
/// Includes query + server-side error description
|
||||||
InternalServerError(&'a str, String),
|
InternalServerError(Option<&'a str>, String),
|
||||||
/// Includes reference to the original request
|
/// Includes reference to the original request
|
||||||
NotFound(&'a str),
|
NotFound(&'a str),
|
||||||
/// Includes bytes array
|
/// Includes bytes array
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::{response::Response, session::Session};
|
use crate::{response::Response, session::Session};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::{
|
use std::{
|
||||||
io::{Read, Write},
|
io::{ErrorKind, Read, Write},
|
||||||
net::{SocketAddr, TcpStream},
|
net::{SocketAddr, TcpStream},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
@ -41,15 +41,11 @@ impl Connection {
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if let Some(ref r) = self.session.request {
|
if let Some(ref request) = self.session.request {
|
||||||
r.add(&self.address.client, &q)
|
request.add(&self.address.client, &q)
|
||||||
}
|
}
|
||||||
if self
|
if self.session.clone().public.request(&q, |response| {
|
||||||
.session
|
self.response(response).is_ok_and(|sent| {
|
||||||
.clone()
|
|
||||||
.public
|
|
||||||
.request(&q, |r| match self.response(r) {
|
|
||||||
Ok(sent) => {
|
|
||||||
t += sent;
|
t += sent;
|
||||||
if self.session.is_debug {
|
if self.session.is_debug {
|
||||||
println!(
|
println!(
|
||||||
|
|
@ -58,16 +54,8 @@ impl Connection {
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
true
|
true
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!(
|
|
||||||
"[{}] > [{}] `{q}`: error sending response: `{e}`",
|
|
||||||
self.address.server, self.address.client
|
|
||||||
);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
{
|
}) {
|
||||||
self.session
|
self.session
|
||||||
.access_log
|
.access_log
|
||||||
.clf(&self.address.client, Some(&q), 0, t);
|
.clf(&self.address.client, Some(&q), 0, t);
|
||||||
|
|
@ -76,10 +64,16 @@ impl Connection {
|
||||||
self.session
|
self.session
|
||||||
.access_log
|
.access_log
|
||||||
.clf(&self.address.client, Some(&q), 1, t);
|
.clf(&self.address.client, Some(&q), 1, t);
|
||||||
|
if self.session.is_debug {
|
||||||
|
println!(
|
||||||
|
"[{}] - [{}] connection closed by client.",
|
||||||
|
self.address.server, self.address.client,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => match self.response(Response::InternalServerError(
|
Err(e) => match self.response(Response::InternalServerError(
|
||||||
"",
|
None,
|
||||||
format!(
|
format!(
|
||||||
"[{}] < [{}] failed to handle incoming request: `{e}`",
|
"[{}] < [{}] failed to handle incoming request: `{e}`",
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
|
|
@ -118,8 +112,8 @@ impl Connection {
|
||||||
Ok(urlencoding::decode(std::str::from_utf8(&b[..n])?.trim())?.to_string())
|
Ok(urlencoding::decode(std::str::from_utf8(&b[..n])?.trim())?.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn response(&mut self, response: Response) -> Result<usize> {
|
fn response(&mut self, response: Response) -> std::io::Result<usize> {
|
||||||
let bytes = match response {
|
let data = match response {
|
||||||
Response::File(b) => b,
|
Response::File(b) => b,
|
||||||
Response::Directory(q, ref s, is_root) => {
|
Response::Directory(q, ref s, is_root) => {
|
||||||
&if is_root {
|
&if is_root {
|
||||||
|
|
@ -138,7 +132,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
Response::InternalServerError(q, e) => {
|
Response::InternalServerError(q, e) => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[{}] > [{}] `{q}`: internal server error: `{e}`",
|
"[{}] > [{}] `{q:?}`: internal server error: `{e}`",
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
);
|
);
|
||||||
self.session.template.internal_server_error()
|
self.session.template.internal_server_error()
|
||||||
|
|
@ -158,9 +152,22 @@ impl Connection {
|
||||||
self.session.template.not_found()
|
self.session.template.not_found()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.stream.write_all(bytes)?;
|
match self.stream.write_all(data) {
|
||||||
|
Ok(()) => {
|
||||||
self.stream.flush()?;
|
self.stream.flush()?;
|
||||||
Ok(bytes.len())
|
Ok(data.len())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// client may close the active connection unexpectedly, ignore some kinds
|
||||||
|
if !matches!(e.kind(), ErrorKind::BrokenPipe | ErrorKind::ConnectionReset) {
|
||||||
|
eprintln!(
|
||||||
|
"[{}] > [{}] error sending response: `{e}`",
|
||||||
|
self.address.server, self.address.client
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shutdown(self) {
|
fn shutdown(self) {
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ impl Public {
|
||||||
Ok(t) => match (t.is_dir(), t.is_file()) {
|
Ok(t) => match (t.is_dir(), t.is_file()) {
|
||||||
(true, _) => callback(match self.list(&p) {
|
(true, _) => callback(match self.list(&p) {
|
||||||
Ok(list) => Response::Directory(query, list, p == self.public_dir),
|
Ok(list) => Response::Directory(query, list, p == self.public_dir),
|
||||||
Err(e) => Response::InternalServerError(query, e.to_string()),
|
Err(e) => Response::InternalServerError(Some(query), e.to_string()),
|
||||||
}),
|
}),
|
||||||
(_, true) => match fs::File::open(p) {
|
(_, true) => match fs::File::open(p) {
|
||||||
Ok(mut f) => loop {
|
Ok(mut f) => loop {
|
||||||
|
|
@ -75,21 +75,21 @@ impl Public {
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return callback(Response::InternalServerError(
|
return callback(Response::InternalServerError(
|
||||||
query,
|
Some(query),
|
||||||
format!("failed to read response chunk: `{e}`"),
|
format!("failed to read response chunk: `{e}`"),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => callback(Response::InternalServerError(
|
Err(e) => callback(Response::InternalServerError(
|
||||||
query,
|
Some(query),
|
||||||
format!("failed to read response: `{e}`"),
|
format!("failed to read response: `{e}`"),
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
_ => panic!(), // unexpected
|
_ => panic!(), // unexpected
|
||||||
},
|
},
|
||||||
Err(e) => callback(Response::InternalServerError(
|
Err(e) => callback(Response::InternalServerError(
|
||||||
query,
|
Some(query),
|
||||||
format!("failed to read storage: `{e}`"),
|
format!("failed to read storage: `{e}`"),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue