Made cache checking easier.
[jsvc.git] / src / dolda / jsvc / util / Cache.java
1 package dolda.jsvc.util;
2
3 import dolda.jsvc.*;
4 import java.util.*;
5
6 public class Cache {
7     public static void checkmtime(Request req, long mtime) {
8         /* Since the HTTP time format is (reasonably enough) precise
9          * only to seconds, any extra milliseconds must be trimmed
10          * off, or the mtime will almost certainly not match. */
11         Date mdate = new Date((mtime / 1000) * 1000);
12         String ims = req.inheaders().get("If-Modified-Since");
13         if(ims != null) {
14             Date cldate;
15             try {
16                 cldate = Http.parsedate(ims);
17             } catch(java.text.ParseException e) {
18                 throw(Restarts.stdresponse(400, "The If-Modified-Since header is not parseable."));
19             }
20             if(mdate.compareTo(cldate) <= 0) {
21                 req.status(304);
22                 req.outheaders().put("Content-Length", "0");
23                 throw(Restarts.done());
24             }
25         }
26         req.outheaders().put("Last-Modified", Http.fmtdate(mdate));
27     }
28 }