Repository: static-web-server/static-web-server (Rust)

Issue:
Server crashes with compression-static enabled and root as "."
**Describe the bug**
The server crashes when it receives a request for "/" with static compression enabled and root as ".".
This only happens if the client supports compression

**To Reproduce**
run `static-web-server --root . --compression-static true` and access http://localhost from a client that supports compression

**Expected behavior**
Server should not crash

**Logs/Screenshots**
```
2022-12-02T02:38:26.080893Z  INFO static_web_server::handler: incoming request: method=GET uri=/
2022-12-02T02:38:26.080900Z TRACE static_web_server::static_files: dir: base=".", route=""
2022-12-02T02:38:26.080912Z TRACE static_web_server::compression_static: preparing pre-compressed file path variant of .
thread 'static-web-server' panicked at 'called `Option::unwrap()` on a `None` value', src/compression_static.rs:34:53
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[1]    368327 IOT instruction  static-web-server -p 8080 --root . --compression-static true -g trace
```

**Environment and Specs**
 - static-web-server: 2.14.0
 - OS: Debian bookworm/sid Linux 6.0.0
 - Client: Firefox 107.0

**Additional context**
Add any other context about the problem here if needed.



Discussion:
Thanks for that I was able to reproduce it.

Server:
```sh
$ static-web-server -p 12345 --root . --compression-static true
thread 'static-web-server' panicked at 'called `Option::unwrap()` on a `None` value', src/compression_static.rs:34:53
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fish: Job 1, 'static-web-server -p 12345 --ro…' terminated by signal SIGABRT (Abort)
```

Client:
```sh
$ curl -H "accept-encoding: gzip, deflate, br" http://localhost:12345
curl: (52) Empty reply from server
```
Resolved on [v2.14.1](https://github.com/static-web-server/static-web-server/releases/tag/v2.14.1)



Patch:
diff --git a/src/compression_static.rs b/src/compression_static.rs
index 1fd92c6e..a5693404 100644
--- a/src/compression_static.rs
+++ b/src/compression_static.rs
@@ -31,20 +31,22 @@ pub async fn precompressed_variant(
     // Try to find the pre-compressed metadata variant for the given file path
     if let Some(ext) = precomp_ext {
         let mut filepath_precomp = file_path;
-        let filename = filepath_precomp.file_name().unwrap().to_str().unwrap();
-        let precomp_file_name = [filename, ".", ext].concat();
-        filepath_precomp.set_file_name(precomp_file_name);
-
-        tracing::trace!(
-            "getting metadata for pre-compressed file variant {}",
-            filepath_precomp.display()
-        );
-
-        if let Ok((meta, _)) = file_metadata(&filepath_precomp) {
-            tracing::trace!("pre-compressed file variant found, serving it directly");
-
-            let encoding = if ext == "gz" { "gzip" } else { ext };
-            precompressed = Some((filepath_precomp, meta, encoding));
+        if let Some(filename) = filepath_precomp.file_name() {
+            let filename = filename.to_str().unwrap();
+            let precomp_file_name = [filename, ".", ext].concat();
+            filepath_precomp.set_file_name(precomp_file_name);
+
+            tracing::trace!(
+                "getting metadata for pre-compressed file variant {}",
+                filepath_precomp.display()
+            );
+
+            if let Ok((meta, _)) = file_metadata(&filepath_precomp) {
+                tracing::trace!("pre-compressed file variant found, serving it directly");
+
+                let encoding = if ext == "gz" { "gzip" } else { ext };
+                precompressed = Some((filepath_precomp, meta, encoding));
+            }
         }
 
         // Note: In error case like "no such file or dir" the workflow just continues


Test patch:
diff --git a/tests/compression_static.rs b/tests/compression_static.rs
index c15de60b..63715ad3 100644
--- a/tests/compression_static.rs
+++ b/tests/compression_static.rs
@@ -126,4 +126,31 @@ mod tests {
             "body and index_gz_buf are not equal in length"
         );
     }
+
+    #[tokio::test]
+    async fn compression_static_base_path_as_dot() {
+        let mut headers = HeaderMap::new();
+        headers.insert(
+            http::header::ACCEPT_ENCODING,
+            "gzip, deflate, br".parse().unwrap(),
+        );
+
+        let base_path = PathBuf::from(".");
+
+        let (_resp, _) = static_files::handle(&HandleOpts {
+            method: &Method::GET,
+            headers: &headers,
+            base_path: &base_path,
+            uri_path: "/",
+            uri_query: None,
+            dir_listing: true,
+            dir_listing_order: 6,
+            dir_listing_format: &DirListFmt::Html,
+            redirect_trailing_slash: true,
+            compression_static: true,
+            ignore_hidden_files: false,
+        })
+        .await
+        .expect("unexpected error response on `handle` function");
+    }
 }