408529 Etags set in 304 response

Improved default servlet setting of etags
This commit is contained in:
Greg Wilkins 2013-06-03 14:15:55 +10:00
parent d581dac287
commit 952f8442e5
1 changed files with 29 additions and 55 deletions

View File

@ -658,7 +658,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
if (ifm!=null) if (ifm!=null)
{ {
boolean match=false; boolean match=false;
if (content!=null && content.getETag()!=null) if (content.getETag()!=null)
{ {
QuotedStringTokenizer quoted = new QuotedStringTokenizer(ifm,", ",false,true); QuotedStringTokenizer quoted = new QuotedStringTokenizer(ifm,", ",false,true);
while (!match && quoted.hasMoreTokens()) while (!match && quoted.hasMoreTokens())
@ -671,48 +671,39 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
if (!match) if (!match)
{ {
Response r = Response.getResponse(response); response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
r.reset(true);
r.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
return false; return false;
} }
} }
String ifnm=request.getHeader(HttpHeader.IF_NONE_MATCH.asString()); String if_non_match_etag=request.getHeader(HttpHeader.IF_NONE_MATCH.asString());
if (ifnm!=null && content!=null && content.getETag()!=null) if (if_non_match_etag!=null && content.getETag()!=null)
{ {
// Look for GzipFiltered version of etag // Look for GzipFiltered version of etag
if (content.getETag().toString().equals(request.getAttribute("o.e.j.s.GzipFilter.ETag"))) if (content.getETag().toString().equals(request.getAttribute("o.e.j.s.GzipFilter.ETag")))
{ {
Response r = Response.getResponse(response); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.reset(true); response.setHeader(HttpHeader.ETAG.asString(),if_non_match_etag);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.getHttpFields().put(HttpHeader.ETAG,ifnm);
return false; return false;
} }
// Handle special case of exact match. // Handle special case of exact match.
if (content.getETag().toString().equals(ifnm)) if (content.getETag().toString().equals(if_non_match_etag))
{ {
Response r = Response.getResponse(response); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.reset(true); response.setHeader(HttpHeader.ETAG.asString(),content.getETag());
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.getHttpFields().put(HttpHeader.ETAG,content.getETag());
return false; return false;
} }
// Handle list of tags // Handle list of tags
QuotedStringTokenizer quoted = new QuotedStringTokenizer(ifnm,", ",false,true); QuotedStringTokenizer quoted = new QuotedStringTokenizer(if_non_match_etag,", ",false,true);
while (quoted.hasMoreTokens()) while (quoted.hasMoreTokens())
{ {
String tag = quoted.nextToken(); String tag = quoted.nextToken();
if (content.getETag().toString().equals(tag)) if (content.getETag().toString().equals(tag))
{ {
Response r = Response.getResponse(response); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.reset(true); response.setHeader(HttpHeader.ETAG.asString(),content.getETag());
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
r.getHttpFields().put(HttpHeader.ETAG,content.getETag());
return false; return false;
} }
} }
@ -727,50 +718,33 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
if (ifms!=null) if (ifms!=null)
{ {
//Get jetty's Response impl //Get jetty's Response impl
Response r = Response.getResponse(response); String mdlm=content.getLastModified();
if (mdlm!=null && ifms.equals(mdlm))
if (content!=null)
{ {
String mdlm=content.getLastModified(); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (mdlm!=null) if (_etags)
{ response.setHeader(HttpHeader.ETAG.asString(),content.getETag());
if (ifms.equals(mdlm)) response.flushBuffer();
{ return false;
r.reset(true);
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
if (_etags)
r.getHttpFields().add(HttpHeader.ETAG,content.getETag());
r.flushBuffer();
return false;
}
}
} }
long ifmsl=request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString()); long ifmsl=request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
if (ifmsl!=-1) if (ifmsl!=-1 && resource.lastModified()/1000 <= ifmsl/1000)
{ {
if (resource.lastModified()/1000 <= ifmsl/1000) response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
{ if (_etags)
r.reset(true); response.setHeader(HttpHeader.ETAG.asString(),content.getETag());
r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.flushBuffer();
if (_etags) return false;
r.getHttpFields().add(HttpHeader.ETAG,content.getETag());
r.flushBuffer();
return false;
}
} }
} }
// Parse the if[un]modified dates and compare to resource // Parse the if[un]modified dates and compare to resource
long date=request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString()); long date=request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString());
if (date!=-1 && resource.lastModified()/1000 > date/1000)
if (date!=-1)
{ {
if (resource.lastModified()/1000 > date/1000) response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
{ return false;
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return false;
}
} }
} }