stage 7 refactoring

This commit is contained in:
YK 2024-05-11 01:20:00 +03:00
parent f4b8eb502d
commit d5e6878703

View File

@ -71,19 +71,18 @@ async fn process (mut stream: TcpStream, args: A) -> Result<()> {
"/user-agent" => Response::TextPlain(headers.get("User-Agent").to_owned()),
// p if let Some(echo) = p.strip_prefix("/echo/") => Response::TextPlain(echo), // a nicer way to do that, not available in stable yet
p if p.starts_with("/echo/") => Response::TextPlain(p.trim_start_matches("/echo/").to_owned()),
p if p.starts_with("/files/") => {
if let Some(path) = &args.directory {
p if p.starts_with("/files/") => 'a : {
let Some(path) = &args.directory else {
break 'a Response::_500;
};
let path = path.join(p.trim_start_matches("/files/"));
let Ok(mut f) = File::open(path).await else {
break 'a Response::_404;
};
let mut buf = vec![];
if let Ok(mut f) = File::open(path).await {
let _ = f.read_to_end(&mut buf).await;
Response::OctetStream(buf)
} else {
Response::_404
}
} else {
Response::_500
}
},
_ => Response::_404,
};