Replaced the "Restarts" class with individual restart classes.
[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         final 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(new ClientError("The If-Modified-Since header is not parseable."));
19             }
20             if(mdate.compareTo(cldate) <= 0) {
21                 throw(new RequestRestart() {
22                         public void respond(Request req) {
23                             req.status(304);
24                             req.outheaders().put("Content-Length", "0");
25                             req.outheaders().put("Last-Modified", Http.fmtdate(mdate));
26                         }
27                     });
28             }
29         }
30         req.outheaders().put("Last-Modified", Http.fmtdate(mdate));
31     }
32 }