update etags for all methods #1416

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-03-13 14:07:37 +11:00
parent 74f5b9898f
commit a933456089
3 changed files with 71 additions and 65 deletions

View File

@ -448,6 +448,28 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
interceptor=interceptor.getNextInterceptor();
}
// Special handling for etags
for (ListIterator<HttpField> fields = baseRequest.getHttpFields().listIterator(); fields.hasNext();)
{
HttpField field = fields.next();
if (field.getHeader()==HttpHeader.IF_NONE_MATCH || field.getHeader()==HttpHeader.IF_MATCH)
{
String etag = field.getValue();
int i=etag.indexOf(CompressedContentFormat.GZIP._etagQuote);
if (i>0)
{
baseRequest.setAttribute("o.e.j.s.h.gzip.GzipHandler.etag",etag);
while (i>=0)
{
etag=etag.substring(0,i)+etag.substring(i+CompressedContentFormat.GZIP._etag.length());
i=etag.indexOf(CompressedContentFormat.GZIP._etagQuote,i);
}
fields.set(new HttpField(field.getHeader(),etag));
}
}
}
// If not a supported method - no Vary because no matter what client, this URI is always excluded
if (!_methods.test(baseRequest.getMethod()))
{
@ -494,28 +516,6 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
}
}
}
// Special handling for etags
for (ListIterator<HttpField> fields = baseRequest.getHttpFields().listIterator(); fields.hasNext();)
{
HttpField field = fields.next();
if (field.getHeader()==HttpHeader.IF_NONE_MATCH || field.getHeader()==HttpHeader.IF_MATCH)
{
String etag = field.getValue();
int i=etag.indexOf(CompressedContentFormat.GZIP._etagQuote);
if (i>0)
{
baseRequest.setAttribute("o.e.j.s.h.gzip.GzipHandler.etag",etag);
while (i>=0)
{
etag=etag.substring(0,i)+etag.substring(i+CompressedContentFormat.GZIP._etag.length());
i=etag.indexOf(CompressedContentFormat.GZIP._etagQuote,i);
}
fields.set(new HttpField(field.getHeader(),etag));
}
}
}
HttpOutput.Interceptor orig_interceptor = out.getInterceptor();
try

View File

@ -1,42 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.server.handler.gzip;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import org.junit.Test;
public class GzipHandlerTest
{
@Test
public void testAddGetPaths()
{
GzipHandler gzip = new GzipHandler();
gzip.addIncludedPaths("/foo");
gzip.addIncludedPaths("^/bar.*$");
String[] includedPaths = gzip.getIncludedPaths();
assertThat("Included Paths.size", includedPaths.length, is(2));
assertThat("Included Paths", Arrays.asList(includedPaths), contains("/foo","^/bar.*$"));
}
}

View File

@ -47,13 +47,13 @@ import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@SuppressWarnings("serial")
public class GzipHandlerTest
{
private static final String __content =
@ -151,6 +151,17 @@ public class GzipHandlerTest
writer.write(__content);
}
}
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
{
String ifm = req.getHeader("If-Match");
if (ifm!=null && ifm.equals(__contentETag))
response.sendError(HttpServletResponse.SC_NO_CONTENT);
else
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
}
}
public static class EchoServlet extends HttpServlet
@ -347,6 +358,43 @@ public class GzipHandlerTest
assertThat(response.get("ETag"),is(__contentETagGzip));
}
@Test
public void testDeleteETagGzipHandler() throws Exception
{
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
request.setMethod("DELETE");
request.setURI("/ctx/content");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("If-Match","WrongEtag--gzip");
request.setHeader("accept-encoding","gzip");
response = HttpTester.parseResponse(_connector.getResponse(request.generate()));
assertThat(response.getStatus(),is(HttpServletResponse.SC_NOT_MODIFIED));
assertThat(response.get("Content-Encoding"),not(Matchers.equalToIgnoringCase("gzip")));
request = HttpTester.newRequest();
request.setMethod("DELETE");
request.setURI("/ctx/content");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("If-Match",__contentETagGzip);
request.setHeader("accept-encoding","gzip");
response = HttpTester.parseResponse(_connector.getResponse(request.generate()));
assertThat(response.getStatus(),is(HttpServletResponse.SC_NO_CONTENT));
assertThat(response.get("Content-Encoding"),not(Matchers.equalToIgnoringCase("gzip")));
}
@Test
public void testForwardGzipHandler() throws Exception
{