stage 7 refactoring
This commit is contained in:
parent
f4b8eb502d
commit
d5e6878703
17
src/main.rs
17
src/main.rs
@ -71,19 +71,18 @@ async fn process (mut stream: TcpStream, args: A) -> Result<()> {
|
|||||||
"/user-agent" => Response::TextPlain(headers.get("User-Agent").to_owned()),
|
"/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 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("/echo/") => Response::TextPlain(p.trim_start_matches("/echo/").to_owned()),
|
||||||
p if p.starts_with("/files/") => {
|
p if p.starts_with("/files/") => 'a : {
|
||||||
if let Some(path) = &args.directory {
|
let Some(path) = &args.directory else {
|
||||||
|
break 'a Response::_500;
|
||||||
|
};
|
||||||
let path = path.join(p.trim_start_matches("/files/"));
|
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![];
|
let mut buf = vec![];
|
||||||
if let Ok(mut f) = File::open(path).await {
|
|
||||||
let _ = f.read_to_end(&mut buf).await;
|
let _ = f.read_to_end(&mut buf).await;
|
||||||
Response::OctetStream(buf)
|
Response::OctetStream(buf)
|
||||||
} else {
|
|
||||||
Response::_404
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Response::_500
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => Response::_404,
|
_ => Response::_404,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user