|
1 | | -extern crate mime = "mime-types"; |
2 | 1 | extern crate conduit; |
| 2 | +extern crate mime = "mime-types"; |
| 3 | +extern crate time; |
3 | 4 |
|
4 | 5 | use std::fmt::Show; |
5 | 6 | use std::collections::HashMap; |
@@ -47,10 +48,17 @@ impl Handler for Static { |
47 | 48 | } |
48 | 49 | }; |
49 | 50 | 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(); |
50 | 56 |
|
51 | 57 | let mut headers = HashMap::new(); |
52 | 58 | headers.insert("Content-Type".to_str(), vec![mime.to_str()]); |
53 | 59 | 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")]); |
54 | 62 |
|
55 | 63 | Ok(Response { |
56 | 64 | status: (200, "OK"), |
@@ -112,4 +120,16 @@ mod tests { |
112 | 120 | let res = handler.call(&mut req).ok().expect("No response"); |
113 | 121 | assert_eq!(res.status.val0(), 404); |
114 | 122 | } |
| 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 | + } |
115 | 135 | } |
0 commit comments