index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2022-11-12 22:18:29.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2022-11-12 22:18:29.0 +00:00:00
commit
9d67d9d42b6f9f4447740e3b511280d4f4c54bb1 [patch]
tree
a5a23d981070163af986219f4a632f8d342200ee
parent
efda2378a0217c6045957cb0b0d6a35107643bba
download
9d67d9d42b6f9f4447740e3b511280d4f4c54bb1.tar.gz

fix: unhandled panic when get "last modified" on dir listing module



Diff

 src/directory_listing.rs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/directory_listing.rs b/src/directory_listing.rs
index aed0d89..788e9a0 100644
--- a/src/directory_listing.rs
+++ b/src/directory_listing.rs
@@ -438,6 +438,7 @@ fn sort_file_entries(files: &mut [FileEntry], order_code: u8) -> SortingAttr<'_>
    }
}

/// Return the last modified `DateTime` in local timescale.
fn parse_last_modified(modified: SystemTime) -> Result<DateTime<Local>> {
    let since_epoch = modified.duration_since(UNIX_EPOCH)?;
    // HTTP times don't have nanosecond precision, so we truncate
@@ -449,10 +450,16 @@ fn parse_last_modified(modified: SystemTime) -> Result<DateTime<Local>> {
    // the modification time of a file with greater than second
    // precision appears to be something that only is possible to
    // do on Linux.
    let utc_dt = NaiveDateTime::from_timestamp(

    let utc_dt = NaiveDateTime::from_timestamp_opt(
        since_epoch.as_secs() as i64,
        since_epoch.subsec_nanos() as u32,
    );
    let local_dt = DateTime::<Utc>::from_utc(utc_dt, Utc).with_timezone(&Local);
    Ok(local_dt)

    match utc_dt {
        Some(utc_dt) => Ok(DateTime::<Utc>::from_utc(utc_dt, Utc).with_timezone(&Local)),
        None => Err(anyhow!(
            "out-of-range number of seconds and/or invalid nanosecond"
        )),
    }
}