richerarc/saphir

[BUG] FileMiddleware issue: HEAD request on a file doesn't cache the file properly

Closed this issue · 1 comments

Description
If you use the FileMiddleware with a cache, there is an issue if you receive a HEAD request on a file before any GET request. The problem is that we save in cache an empty file since we don't read it so we save an empty file in cache. All GET request receive after don't return the file as expected.

(Of course, the file has to be cached. Depending of the cache configuration, it could work or not. But for sure, you can get cases that don't work)

Expected behavior
GET request on a file should return the file, even if we have receive a HEAD request before.

Additional info (please complete the following information):

  • Saphir Version: 2.8

I'm not sure it is the right fix, but we could validate that we have read the entire file before saving it to the cache.
https://github.com/richerarc/saphir/blob/master/saphir/src/file/cache.rs#L263

impl Drop for FileCacher {
    fn drop(&mut self) {
        if self.inner.get_size() == self.buff.len() as u64 {
            self.save_file_to_cache();
        }
    }
}