mirror of
https://github.com/YGGverse/nexy.git
synced 2026-03-31 17:25:27 +00:00
give semantical names to response members
This commit is contained in:
parent
90b8b8c921
commit
23c9c1b96e
3 changed files with 53 additions and 36 deletions
|
|
@ -1,13 +1,19 @@
|
||||||
/// Internal server response types
|
/// Internal server response types
|
||||||
pub enum Response<'a> {
|
pub enum Response<'a> {
|
||||||
/// Includes reference to the original request
|
AccessDenied {
|
||||||
AccessDenied(&'a str),
|
query: &'a str,
|
||||||
/// Includes query + server-side error description
|
},
|
||||||
InternalServerError(Option<&'a str>, String),
|
InternalServerError {
|
||||||
/// Includes reference to the original request
|
query: Option<&'a str>,
|
||||||
NotFound(&'a str),
|
error: String,
|
||||||
/// Includes bytes array
|
},
|
||||||
|
NotFound {
|
||||||
|
query: &'a str,
|
||||||
|
},
|
||||||
File(&'a [u8]),
|
File(&'a [u8]),
|
||||||
/// Includes query, list + is public root directory status
|
Directory {
|
||||||
Directory(&'a str, String, bool),
|
query: &'a str,
|
||||||
|
data: String,
|
||||||
|
is_root: bool,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,13 +72,13 @@ impl Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => match self.response(Response::InternalServerError(
|
Err(e) => match self.response(Response::InternalServerError {
|
||||||
None,
|
query: None,
|
||||||
format!(
|
error: 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
|
||||||
),
|
),
|
||||||
)) {
|
}) {
|
||||||
Ok(sent) => {
|
Ok(sent) => {
|
||||||
t += sent;
|
t += sent;
|
||||||
if self.session.is_debug {
|
if self.session.is_debug {
|
||||||
|
|
@ -115,7 +115,11 @@ impl Connection {
|
||||||
fn response(&mut self, response: Response) -> std::io::Result<usize> {
|
fn response(&mut self, response: Response) -> std::io::Result<usize> {
|
||||||
let data = match response {
|
let data = match response {
|
||||||
Response::File(b) => b,
|
Response::File(b) => b,
|
||||||
Response::Directory(q, ref s, is_root) => {
|
Response::Directory {
|
||||||
|
query: q,
|
||||||
|
data: ref s,
|
||||||
|
is_root,
|
||||||
|
} => {
|
||||||
&if is_root {
|
&if is_root {
|
||||||
self.session.template.welcome(
|
self.session.template.welcome(
|
||||||
Some(s),
|
Some(s),
|
||||||
|
|
@ -130,23 +134,23 @@ impl Connection {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Response::InternalServerError(q, e) => {
|
Response::InternalServerError { query, error } => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[{}] > [{}] `{q:?}`: internal server error: `{e}`",
|
"[{}] > [{}] `{query:?}`: internal server error: `{error}`",
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
);
|
);
|
||||||
self.session.template.internal_server_error()
|
self.session.template.internal_server_error()
|
||||||
}
|
}
|
||||||
Response::AccessDenied(q) => {
|
Response::AccessDenied { query } => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[{}] < [{}] access to `{q}` denied.",
|
"[{}] < [{}] access to `{query}` denied.",
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
);
|
);
|
||||||
self.session.template.access_denied()
|
self.session.template.access_denied()
|
||||||
}
|
}
|
||||||
Response::NotFound(q) => {
|
Response::NotFound { query } => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[{}] < [{}] requested resource `{q}` not found.",
|
"[{}] < [{}] requested resource `{query}` not found.",
|
||||||
self.address.server, self.address.client
|
self.address.server, self.address.client
|
||||||
);
|
);
|
||||||
self.session.template.not_found()
|
self.session.template.not_found()
|
||||||
|
|
|
||||||
|
|
@ -50,18 +50,25 @@ impl Public {
|
||||||
match p.canonicalize() {
|
match p.canonicalize() {
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
if !c.starts_with(&self.public_dir) {
|
if !c.starts_with(&self.public_dir) {
|
||||||
return callback(Response::AccessDenied(query));
|
return callback(Response::AccessDenied { query });
|
||||||
}
|
}
|
||||||
c
|
c
|
||||||
}
|
}
|
||||||
Err(_) => return callback(Response::NotFound(query)),
|
Err(_) => return callback(Response::NotFound { query }),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match fs::metadata(&p) {
|
match fs::metadata(&p) {
|
||||||
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(data) => Response::Directory {
|
||||||
Err(e) => Response::InternalServerError(Some(query), e.to_string()),
|
query,
|
||||||
|
data,
|
||||||
|
is_root: p == self.public_dir,
|
||||||
|
},
|
||||||
|
Err(e) => Response::InternalServerError {
|
||||||
|
query: Some(query),
|
||||||
|
error: e.to_string(),
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
(_, true) => match fs::File::open(p) {
|
(_, true) => match fs::File::open(p) {
|
||||||
Ok(mut f) => loop {
|
Ok(mut f) => loop {
|
||||||
|
|
@ -74,24 +81,24 @@ impl Public {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return callback(Response::InternalServerError(
|
return callback(Response::InternalServerError {
|
||||||
Some(query),
|
query: Some(query),
|
||||||
format!("failed to read response chunk: `{e}`"),
|
error: format!("failed to read response chunk: `{e}`"),
|
||||||
));
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => callback(Response::InternalServerError(
|
Err(e) => callback(Response::InternalServerError {
|
||||||
Some(query),
|
query: Some(query),
|
||||||
format!("failed to read response: `{e}`"),
|
error: format!("failed to read response: `{e}`"),
|
||||||
)),
|
}),
|
||||||
},
|
},
|
||||||
_ => panic!(), // unexpected
|
_ => panic!(), // unexpected
|
||||||
},
|
},
|
||||||
Err(e) => callback(Response::InternalServerError(
|
Err(e) => callback(Response::InternalServerError {
|
||||||
Some(query),
|
query: Some(query),
|
||||||
format!("failed to read storage: `{e}`"),
|
error: format!("failed to read storage: `{e}`"),
|
||||||
)),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue