Browse Source

Use to_string_lossy on possibly non-Unicode inputs

Signed-off-by: Alexey Yerin <yerinalexey98fd@gmail.com>
master
Alexey Yerin 4 years ago committed by Garrit Franke
parent
commit
be28fe5d0e
  1. 3
      src/error.rs
  2. 16
      src/main.rs

3
src/error.rs

@ -15,9 +15,6 @@ pub enum TaurusError {
#[error("failed to bind: {0}")]
BindFailed(io::Error),
#[error("invalid Unicode input")]
InvalidUnicode,
#[error("could not read the stream")]
StreamReadFailed(io::Error),

16
src/main.rs

@ -104,11 +104,9 @@ fn read_file(file_path: &str) -> Result<Vec<u8>, io::Error> {
fn write_file(path: &str) -> Result<GeminiResponse, error::TaurusError> {
let extension = path::Path::new(path)
.extension()
.unwrap_or_else(|| std::ffi::OsStr::new(""))
.to_str()
.ok_or(error::TaurusError::InvalidUnicode)?;
.unwrap_or_else(|| std::ffi::OsStr::new(""));
let mime_type = match extension {
let mime_type = match &*extension.to_string_lossy() {
"gmi" => "text/gemini; charset=utf-8",
ext => mime_guess::from_ext(ext)
.first_raw()
@ -161,15 +159,9 @@ fn handle_client(
if path.exists() {
// If it's a directory, try to find index.gmi
if path.is_dir() {
let index_path = path
.join("index.gmi")
.to_str()
.ok_or(error::TaurusError::InvalidUnicode)?
.to_owned();
write_file(&index_path)?.send(stream)
write_file(&path.join("index.gmi").to_string_lossy())?.send(stream)
} else {
write_file(path.to_str().ok_or(error::TaurusError::InvalidUnicode)?)?.send(stream)
write_file(&path.to_string_lossy())?.send(stream)
}
} else {
GeminiResponse::not_found().send(stream)

Loading…
Cancel
Save