Skip to content

Commit

Permalink
Skip headers for Not Modified fs responses (#205)
Browse files Browse the repository at this point in the history
According to RFC 7232, 304 Not Modified responses should not generate
representation metadata other than the following fields: Cache-Control,
Content-Location, Date, ETag, Expires, and Vary.

This avoids a warning from hyper when logging is enabled:
"content-length value found, but empty body provided". While it is
allowed to provide Content-Length for Not Modified responses it is
unnecessary.
  • Loading branch information
KamilaBorowska authored and seanmonstar committed Apr 15, 2019
1 parent 2407776 commit 4a1c67c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/filters/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn file_conditional(
let mut len = meta.len();
let modified = meta.modified().ok().map(LastModified::from);

let mut resp = match conditionals.check(modified) {
let resp = match conditionals.check(modified) {
Cond::NoBody(resp) => resp,
Cond::WithBody(range) => {
bytes_range(range, len)
Expand All @@ -317,6 +317,16 @@ fn file_conditional(
len = sub_len;
}

let mime = mime_guess::guess_mime_type(path.as_ref());

resp.headers_mut().typed_insert(ContentLength(len));
resp.headers_mut().typed_insert(ContentType::from(mime));
resp.headers_mut().typed_insert(AcceptRanges::bytes());

if let Some(last_modified) = modified {
resp.headers_mut().typed_insert(last_modified);
}

resp
})
.unwrap_or_else(|BadRange| {
Expand All @@ -330,18 +340,6 @@ fn file_conditional(
}
};

if resp.status() != StatusCode::RANGE_NOT_SATISFIABLE {
let mime = mime_guess::guess_mime_type(path.as_ref());

resp.headers_mut().typed_insert(ContentLength(len));
resp.headers_mut().typed_insert(ContentType::from(mime));
resp.headers_mut().typed_insert(AcceptRanges::bytes());

if let Some(last_modified) = modified {
resp.headers_mut().typed_insert(last_modified);
}
}

File { resp }
})
}
Expand Down
6 changes: 5 additions & 1 deletion tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ fn not_modified() {
let file = warp::fs::file("README.md");

let req = warp::test::request();
let body = fs::read("README.md").unwrap();
let res1 = req.reply(&file);
assert_eq!(res1.status(), 200);
assert_eq!(res1.headers()["content-length"], body.len().to_string());

// if-modified-since
let res = warp::test::request()
.header("if-modified-since", &res1.headers()["last-modified"])
.reply(&file);
assert_eq!(res.headers().get("content-length"), None);
assert_eq!(res.status(), 304);
assert_eq!(res.body(), "");

Expand All @@ -133,7 +136,8 @@ fn not_modified() {
.header("if-modified-since", "Sun, 07 Nov 1994 01:00:00 GMT")
.reply(&file);
assert_eq!(res.status(), 200);
assert_eq!(res.body(), &*fs::read("README.md").unwrap());
assert_eq!(res.body(), &body);
assert_eq!(res1.headers()["content-length"], body.len().to_string());
}

#[test]
Expand Down

0 comments on commit 4a1c67c

Please sign in to comment.