private final boolean dir;
private final String mimetype;
- public StaticContent(Class<?> base, String resname, boolean dir, String mimetype) {
+ public StaticContent(Class<?> base, String resname, String mimetype) {
this.base = base;
- this.resname = resname;
- this.dir = dir;
+ this.dir = ((this.resname = resname).charAt(resname.length() - 1) == '/');
this.mimetype = mimetype;
}
- public StaticContent(String resname, boolean dir, String mimetype) {
- this(null, resname, dir, mimetype);
+ public StaticContent(String resname, String mimetype) {
+ this(null, resname, mimetype);
}
public void respond(Request req) {
String nm;
- if(dir) {
- nm = resname + "/" + req.path();
- } else {
+ if(dir)
+ nm = resname + req.path();
+ else
nm = resname;
- }
InputStream in;
if(base == null) {
in = StaticContent.class.getClassLoader().getResourceAsStream(nm);
}
if(in == null)
throw(Restarts.stdresponse(404));
- String ims = req.inheaders().get("If-Modified-Since");
- Date mtime = new Date((req.ctx().starttime() / 1000) * 1000);
- if(ims != null) {
- Date d;
- try {
- d = Http.parsedate(ims);
- } catch(java.text.ParseException e) {
- throw(Restarts.stdresponse(400));
- }
- if(mtime.compareTo(d) <= 0) {
- req.status(304);
- req.outheaders().put("Content-Length", "0");
- return;
- }
- }
+ Cache.checkmtime(req, req.ctx().starttime());
try {
try {
req.outheaders().put("Content-Type", mimetype);
- req.outheaders().put("Last-Modified", Http.fmtdate(mtime));
Misc.cpstream(in, req.output());
} finally {
in.close();