I'm using a custom web server built on NanoHTTPD, that I've added caching features to. There's only one part in the code that could throw a 304 Not Modified response:
if(isNotModified(f,header,parms)){// if requested file has not been modified, let's make the browser use it's cache
Response r= new Response( Response.Status.NOT_MODIFIED, MIME_PLAINTEXT,(String)null,null);
//r.addHeader(key,val);
r.addHeader("Last-Modified",""+f.lastModified());//FIXME: doesnt work with wget + prob others as well
r.addHeader("ETag",f.getPath()+f.lastModified());
return r;
}
isNotModified method:
/**
* check if the file has been modified since the last time client requested it
* */
public boolean isNotModified(File ff,Properties header, Properties params){
if(params.getProperty("bust")!= null ||params.getProperty("cachebust")!=null)
return false;
if(ff.getName().endsWith(".pg"))
return false;
if(ff.getPath().indexOf("/api/")>=0)
return false;
if("no-cache".equalsIgnoreCase(header.getProperty("cache-control")))
return false;
String mod = header.getProperty("if-modified-since");//names are converted to lowercase fwiw
if(mod==null)//this should happen whenever browser sends no if-modified-since
return false;
try{
long l = Long.parseLong(mod);
if(ff.lastModified()<=l)
return true;
}catch(Exception ex){
ex.printStackTrace();
}
return false;
}
For some reason I'm still getting 304 with (at least) Chrome even though there is no If-Modified-Since specified in the header (or anything related to caching). This leads to the browser interpreting the resource as an empty string and breaking things.