482243 Fixed GzipHandler for Include

This commit is contained in:
Greg Wilkins 2015-11-18 15:44:36 +11:00
parent 486e890d5d
commit 8030a4c783
2 changed files with 105 additions and 20 deletions

View File

@ -29,6 +29,7 @@ import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.ByteArrayOutputStream2; import org.eclipse.jetty.util.ByteArrayOutputStream2;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -240,10 +241,12 @@ public abstract class AbstractCompressedStream extends ServletOutputStream
if (_encoding!=null) if (_encoding!=null)
{ {
setHeader("Content-Encoding", _encoding); String prefix=Response.getResponse(_response).isIncluding()?Response.SET_INCLUDE_HEADER_PREFIX:"";
setHeader(prefix+"Content-Encoding", _encoding);
if (_response.containsHeader("Content-Encoding")) if (_response.containsHeader("Content-Encoding"))
{ {
addHeader("Vary",_vary); addHeader(prefix+"Vary",_vary);
_out=_compressedOutputStream=createStream(); _out=_compressedOutputStream=createStream();
if (_out!=null) if (_out!=null)
{ {
@ -258,9 +261,9 @@ public abstract class AbstractCompressedStream extends ServletOutputStream
{ {
int end = etag.length()-1; int end = etag.length()-1;
if (etag.charAt(end)=='"') if (etag.charAt(end)=='"')
setHeader("ETag",etag.substring(0,end)+"--"+_encoding+'"'); setHeader(prefix+"ETag",etag.substring(0,end)+"--"+_encoding+'"');
else else
setHeader("ETag",etag+"--"+_encoding); setHeader(prefix+"ETag",etag+"--"+_encoding);
} }
return; return;
} }

View File

@ -16,7 +16,7 @@
// ======================================================================== // ========================================================================
// //
package org.eclipse.jetty.embedded; package org.eclipse.jetty.servlets.gzip;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -29,6 +29,7 @@ import java.io.PrintWriter;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -38,6 +39,9 @@ import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlets.gzip.GzipHandler; import org.eclipse.jetty.servlets.gzip.GzipHandler;
import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.IO;
import org.junit.After; import org.junit.After;
@ -60,6 +64,8 @@ public class GzipHandlerTest
"Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse "+ "Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse "+
"et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque."; "et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque.";
private static String __icontent = "BEFORE"+__content+"AFTER";
private Server _server; private Server _server;
private LocalConnector _connector; private LocalConnector _connector;
@ -70,24 +76,50 @@ public class GzipHandlerTest
_connector = new LocalConnector(_server); _connector = new LocalConnector(_server);
_server.addConnector(_connector); _server.addConnector(_connector);
Handler testHandler = new AbstractHandler() GzipHandler gzipHandler = new GzipHandler();
ServletContextHandler context = new ServletContextHandler(gzipHandler,"/ctx");
ServletHandler servlets = context.getServletHandler();
_server.setHandler(gzipHandler);
gzipHandler.setHandler(context);
context.setHandler(servlets);
servlets.addServletWithMapping(TestServlet.class,"/content");
servlets.addServletWithMapping(ForwardServlet.class,"/forward");
servlets.addServletWithMapping(IncludeServlet.class,"/include");
_server.start();
}
public static class TestServlet extends HttpServlet
{ {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException @Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
{ {
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.write(__content); writer.write(__content);
writer.close();
baseRequest.setHandled(true);
} }
}; }
GzipHandler gzipHandler = new GzipHandler(); public static class ForwardServlet extends HttpServlet
gzipHandler.setHandler(testHandler); {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
getServletContext().getRequestDispatcher("/content").forward(request,response);
}
}
_server.setHandler(gzipHandler); public static class IncludeServlet extends HttpServlet
_server.start(); {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.getWriter().write("BEFORE");
getServletContext().getRequestDispatcher("/content").include(request,response);
response.getWriter().write("AFTER");
}
} }
@After @After
@ -108,7 +140,7 @@ public class GzipHandlerTest
request.setVersion("HTTP/1.0"); request.setVersion("HTTP/1.0");
request.setHeader("Host","tester"); request.setHeader("Host","tester");
request.setHeader("accept-encoding","gzip"); request.setHeader("accept-encoding","gzip");
request.setURI("/"); request.setURI("/ctx/content");
response = HttpTester.parseResponse(_connector.getResponses(request.generate())); response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
@ -122,4 +154,54 @@ public class GzipHandlerTest
assertEquals(__content, testOut.toString("UTF8")); assertEquals(__content, testOut.toString("UTF8"));
} }
@Test
public void testForwardGzipHandler() throws Exception
{
// generated and parsed test
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
request.setMethod("GET");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("accept-encoding","gzip");
request.setURI("/ctx/forward");
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
assertTrue(response.get("Content-Encoding").equalsIgnoreCase("gzip"));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
InputStream testIn = new GZIPInputStream(new ByteArrayInputStream(response.getContentBytes()));
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
IO.copy(testIn,testOut);
assertEquals(__content, testOut.toString("UTF8"));
}
@Test
public void testIncludeGzipHandler() throws Exception
{
// generated and parsed test
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
request.setMethod("GET");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("accept-encoding","gzip");
request.setURI("/ctx/include");
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
assertTrue(response.get("Content-Encoding").equalsIgnoreCase("gzip"));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
InputStream testIn = new GZIPInputStream(new ByteArrayInputStream(response.getContentBytes()));
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
IO.copy(testIn,testOut);
assertEquals(__icontent, testOut.toString("UTF8"));
}
} }