Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit acfb229

Browse files
committedJul 6, 2014
Add a Last-Modified header
1 parent 33cf5a4 commit acfb229

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed
 

‎src/conduit-static.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
extern crate mime = "mime-types";
21
extern crate conduit;
2+
extern crate mime = "mime-types";
3+
extern crate time;
34

45
use std::fmt::Show;
56
use std::collections::HashMap;
@@ -47,10 +48,17 @@ impl Handler for Static {
4748
}
4849
};
4950
let stat = try!(file.stat().map_err(|e| box e as Box<Show>));
51+
let ts = time::Timespec {
52+
sec: (stat.modified as i64) / 1000,
53+
nsec: ((stat.modified as i32) % 1000) * 1000
54+
};
55+
let tm = time::at(ts).to_utc();
5056

5157
let mut headers = HashMap::new();
5258
headers.insert("Content-Type".to_str(), vec![mime.to_str()]);
5359
headers.insert("Content-Length".to_str(), vec![stat.size.to_str()]);
60+
headers.insert("Last-Modified".to_str(),
61+
vec![tm.strftime("%a, %d %b %Y %T GMT")]);
5462

5563
Ok(Response {
5664
status: (200, "OK"),
@@ -112,4 +120,16 @@ mod tests {
112120
let res = handler.call(&mut req).ok().expect("No response");
113121
assert_eq!(res.status.val0(), 404);
114122
}
123+
124+
#[test]
125+
fn last_modified() {
126+
let td = TempDir::new("conduit-static").unwrap();
127+
let root = td.path();
128+
File::create(&root.join("test")).unwrap();
129+
let handler = Static::new(root.clone());
130+
let mut req = test::MockRequest::new(conduit::Get, "/test");
131+
let res = handler.call(&mut req).ok().expect("No response");
132+
assert_eq!(res.status.val0(), 200);
133+
assert!(res.headers.find_equiv(&"Last-Modified").is_some());
134+
}
115135
}

0 commit comments

Comments
 (0)
Please sign in to comment.