diff --git a/source/net/yacy/http/Jetty8YaCyDefaultServlet.java b/source/net/yacy/http/Jetty8YaCyDefaultServlet.java index 721570af4..f55386497 100644 --- a/source/net/yacy/http/Jetty8YaCyDefaultServlet.java +++ b/source/net/yacy/http/Jetty8YaCyDefaultServlet.java @@ -38,15 +38,11 @@ import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.util.ConcurrentLog; import net.yacy.kelondro.util.FileUtils; -import org.eclipse.jetty.http.HttpContent; import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.http.HttpMethods; -import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.WriterOutputStream; import org.eclipse.jetty.server.AbstractHttpConnection; import org.eclipse.jetty.server.HttpOutput; import org.eclipse.jetty.server.InclusiveByteRange; -import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.MultiPartOutputStream; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.resource.Resource; @@ -218,64 +214,6 @@ public class Jetty8YaCyDefaultServlet extends YaCyDefaultServlet { } } - /* ------------------------------------------------------------ */ - /* Check modification date headers. - */ - @Override - protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource) - throws IOException { - try { - if (!request.getMethod().equals(HttpMethods.HEAD)) { - HttpContent content = new HttpContent.ResourceAsHttpContent(resource, _mimeTypes.getMimeByExtension(resource.toString()), response.getBufferSize()); - - String ifms = request.getHeader(HttpHeaders.IF_MODIFIED_SINCE); - if (ifms != null) { - //Get jetty's Response impl - Response r = Response.getResponse(response); - - if (content != null) { - Buffer mdlm = content.getLastModified(); - if (mdlm != null) { - if (ifms.equals(mdlm.toString())) { - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - r.flushBuffer(); - return false; - } - } - } - - long ifmsl = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); - if (ifmsl != -1) { - if (resource.lastModified() / 1000 <= ifmsl / 1000) { - r.reset(true); - r.setStatus(HttpServletResponse.SC_NOT_MODIFIED); - r.flushBuffer(); - return false; - } - } - } - - // Parse the if[un]modified dates and compare to resource - long date = request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE); - - if (date != -1) { - if (resource.lastModified() / 1000 > date / 1000) { - response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); - return false; - } - } - if (content != null) content.release(); - } - } catch (IllegalArgumentException iae) { - if (!response.isCommitted()) { - response.sendError(400, iae.getMessage()); - } - throw iae; - } - return true; - } - /* ------------------------------------------------------------ */ @Override protected void sendData(HttpServletRequest request, diff --git a/source/net/yacy/http/YaCyDefaultServlet.java b/source/net/yacy/http/YaCyDefaultServlet.java index 2307214ae..44b78f35b 100644 --- a/source/net/yacy/http/YaCyDefaultServlet.java +++ b/source/net/yacy/http/YaCyDefaultServlet.java @@ -68,6 +68,8 @@ import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.eclipse.jetty.http.HttpHeaders; +import org.eclipse.jetty.http.HttpMethods; import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.io.Buffer; @@ -267,9 +269,45 @@ public abstract class YaCyDefaultServlet extends HttpServlet { } /* ------------------------------------------------------------ */ /* Check modification date headers. + * send a 304 response instead of content if not modified since */ - abstract protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource) - throws IOException; + protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource) + throws IOException { + try { + if (!request.getMethod().equals(HttpMethods.HEAD)) { + + String ifms = request.getHeader(HttpHeaders.IF_MODIFIED_SINCE); + if (ifms != null) { + + long ifmsl = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); + if (ifmsl != -1) { + if (resource.lastModified() / 1000 <= ifmsl / 1000) { + response.reset(); + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + response.flushBuffer(); + return false; + } + } + } + + // Parse the if[un]modified dates and compare to resource + long date = request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE); + + if (date != -1) { + if (resource.lastModified() / 1000 > date / 1000) { + response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED); + return false; + } + } + } + } catch (IllegalArgumentException iae) { + if (!response.isCommitted()) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.getMessage()); + } + throw iae; + } + return true; + } /* ------------------------------------------------------------------- */ protected void sendDirectory(HttpServletRequest request,