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
|
||||
pub enum Response<'a> {
|
||||
/// Includes reference to the original request
|
||||
AccessDenied(&'a str),
|
||||
/// Includes query + server-side error description
|
||||
InternalServerError(Option<&'a str>, String),
|
||||
/// Includes reference to the original request
|
||||
NotFound(&'a str),
|
||||
/// Includes bytes array
|
||||
AccessDenied {
|
||||
query: &'a str,
|
||||
},
|
||||
InternalServerError {
|
||||
query: Option<&'a str>,
|
||||
error: String,
|
||||
},
|
||||
NotFound {
|
||||
query: &'a str,
|
||||
},
|
||||
File(&'a [u8]),
|
||||
/// Includes query, list + is public root directory status
|
||||
Directory(&'a str, String, bool),
|
||||
Directory {
|
||||
query: &'a str,
|
||||
data: String,
|
||||
is_root: bool,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,13 +72,13 @@ impl Connection {
|
|||
}
|
||||
}
|
||||
}
|
||||
Err(e) => match self.response(Response::InternalServerError(
|
||||
None,
|
||||
format!(
|
||||
Err(e) => match self.response(Response::InternalServerError {
|
||||
query: None,
|
||||
error: format!(
|
||||
"[{}] < [{}] failed to handle incoming request: `{e}`",
|
||||
self.address.server, self.address.client
|
||||
),
|
||||
)) {
|
||||
}) {
|
||||
Ok(sent) => {
|
||||
t += sent;
|
||||
if self.session.is_debug {
|
||||
|
|
@ -115,7 +115,11 @@ impl Connection {
|
|||
fn response(&mut self, response: Response) -> std::io::Result<usize> {
|
||||
let data = match response {
|
||||
Response::File(b) => b,
|
||||
Response::Directory(q, ref s, is_root) => {
|
||||
Response::Directory {
|
||||
query: q,
|
||||
data: ref s,
|
||||
is_root,
|
||||
} => {
|
||||
&if is_root {
|
||||
self.session.template.welcome(
|
||||
Some(s),
|
||||
|
|
@ -130,23 +134,23 @@ impl Connection {
|
|||
)
|
||||
}
|
||||
}
|
||||
Response::InternalServerError(q, e) => {
|
||||
Response::InternalServerError { query, error } => {
|
||||
eprintln!(
|
||||
"[{}] > [{}] `{q:?}`: internal server error: `{e}`",
|
||||
"[{}] > [{}] `{query:?}`: internal server error: `{error}`",
|
||||
self.address.server, self.address.client
|
||||
);
|
||||
self.session.template.internal_server_error()
|
||||
}
|
||||
Response::AccessDenied(q) => {
|
||||
Response::AccessDenied { query } => {
|
||||
eprintln!(
|
||||
"[{}] < [{}] access to `{q}` denied.",
|
||||
"[{}] < [{}] access to `{query}` denied.",
|
||||
self.address.server, self.address.client
|
||||
);
|
||||
self.session.template.access_denied()
|
||||
}
|
||||
Response::NotFound(q) => {
|
||||
Response::NotFound { query } => {
|
||||
eprintln!(
|
||||
"[{}] < [{}] requested resource `{q}` not found.",
|
||||
"[{}] < [{}] requested resource `{query}` not found.",
|
||||
self.address.server, self.address.client
|
||||
);
|
||||
self.session.template.not_found()
|
||||
|
|
|
|||
|
|
@ -50,18 +50,25 @@ impl Public {
|
|||
match p.canonicalize() {
|
||||
Ok(c) => {
|
||||
if !c.starts_with(&self.public_dir) {
|
||||
return callback(Response::AccessDenied(query));
|
||||
return callback(Response::AccessDenied { query });
|
||||
}
|
||||
c
|
||||
}
|
||||
Err(_) => return callback(Response::NotFound(query)),
|
||||
Err(_) => return callback(Response::NotFound { query }),
|
||||
}
|
||||
};
|
||||
match fs::metadata(&p) {
|
||||
Ok(t) => match (t.is_dir(), t.is_file()) {
|
||||
(true, _) => callback(match self.list(&p) {
|
||||
Ok(list) => Response::Directory(query, list, p == self.public_dir),
|
||||
Err(e) => Response::InternalServerError(Some(query), e.to_string()),
|
||||
Ok(data) => Response::Directory {
|
||||
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) {
|
||||
Ok(mut f) => loop {
|
||||
|
|
@ -74,24 +81,24 @@ impl Public {
|
|||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return callback(Response::InternalServerError(
|
||||
Some(query),
|
||||
format!("failed to read response chunk: `{e}`"),
|
||||
));
|
||||
return callback(Response::InternalServerError {
|
||||
query: Some(query),
|
||||
error: format!("failed to read response chunk: `{e}`"),
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => callback(Response::InternalServerError(
|
||||
Some(query),
|
||||
format!("failed to read response: `{e}`"),
|
||||
)),
|
||||
Err(e) => callback(Response::InternalServerError {
|
||||
query: Some(query),
|
||||
error: format!("failed to read response: `{e}`"),
|
||||
}),
|
||||
},
|
||||
_ => panic!(), // unexpected
|
||||
},
|
||||
Err(e) => callback(Response::InternalServerError(
|
||||
Some(query),
|
||||
format!("failed to read storage: `{e}`"),
|
||||
)),
|
||||
Err(e) => callback(Response::InternalServerError {
|
||||
query: Some(query),
|
||||
error: format!("failed to read storage: `{e}`"),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue