Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Gorovoy2011-08-08 22:03:36 +0000
committerMichael Gorovoy2011-08-08 22:03:36 +0000
commitd496a4f80af520d1dbce88ba3581bd7807a4c278 (patch)
tree789a24cfd7a88b80511f31936a0128804f8839fe
parent5e2583084d22fd316aa3da3d6baf915f9ab96de7 (diff)
downloadorg.eclipse.jetty.project-d496a4f80af520d1dbce88ba3581bd7807a4c278.tar.gz
org.eclipse.jetty.project-d496a4f80af520d1dbce88ba3581bd7807a4c278.tar.xz
org.eclipse.jetty.project-d496a4f80af520d1dbce88ba3581bd7807a4c278.zip
354014 Content-Length is passed to wrapped response in GZipFilter
-rw-r--r--VERSION.txt1
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipResponseWrapper.java38
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipStream.java19
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest.java54
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest1.java56
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest2.java54
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest3.java56
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest4.java54
8 files changed, 303 insertions, 29 deletions
diff --git a/VERSION.txt b/VERSION.txt
index a9e362bbb3..81f1e50a8b 100644
--- a/VERSION.txt
+++ b/VERSION.txt
@@ -15,6 +15,7 @@ jetty-7.5.0-SNAPSHOT
+ 353465 JAASLoginService ignores callbackHandlerClass
+ 353563 HttpDestinationQueueTest too slow
+ 353862 Improve performance of QuotedStringTokenizer.quote()
+ + 354014 Content-Length is passed to wrapped response in GZipFilter
jetty-7.4.4.v20110707 July 7th 2011
+ 308851 Converted all jetty-client module tests to JUnit 4
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipResponseWrapper.java b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipResponseWrapper.java
index e53f20ee23..ce8f766d9f 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipResponseWrapper.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipResponseWrapper.java
@@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException;
import java.util.Set;
import javax.servlet.ServletOutputStream;
+import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
@@ -34,10 +35,13 @@ import org.eclipse.jetty.util.StringUtil;
*/
public class GzipResponseWrapper extends HttpServletResponseWrapper
{
+ public static int DEFAULT_BUFFER_SIZE = 8192;
+ public static int DEFAULT_MIN_GZIP_SIZE = 256;
+
private HttpServletRequest _request;
private Set<String> _mimeTypes;
- private int _bufferSize=8192;
- private int _minGzipSize=256;
+ private int _bufferSize=DEFAULT_BUFFER_SIZE;
+ private int _minGzipSize=DEFAULT_MIN_GZIP_SIZE;
private PrintWriter _writer;
private GzipStream _gzStream;
@@ -138,11 +142,29 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
*/
public void setContentLength(int length)
{
+ setContentLength((long)length);
+ }
+
+ /* ------------------------------------------------------------ */
+ protected void setContentLength(long length)
+ {
_contentLength=length;
if (_gzStream!=null)
_gzStream.setContentLength(length);
+ else if (_noGzip && _contentLength>=0)
+ {
+ HttpServletResponse response = (HttpServletResponse)getResponse();
+ if(_contentLength<Integer.MAX_VALUE)
+ {
+ response.setContentLength((int)_contentLength);
+ }
+ else
+ {
+ response.setHeader("Content-Length", Long.toString(_contentLength));
+ }
+ }
}
-
+
/* ------------------------------------------------------------ */
/**
* @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
@@ -179,9 +201,7 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
{
if ("content-length".equalsIgnoreCase(name))
{
- _contentLength=Long.parseLong(value);
- if (_gzStream!=null)
- _gzStream.setContentLength(_contentLength);
+ setContentLength(Long.parseLong(value));
}
else if ("content-type".equalsIgnoreCase(name))
{
@@ -296,7 +316,10 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
if (_gzStream==null)
{
if (getResponse().isCommitted() || _noGzip)
+ {
+ setContentLength(_contentLength);
return getResponse().getOutputStream();
+ }
_gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
}
@@ -318,7 +341,10 @@ public class GzipResponseWrapper extends HttpServletResponseWrapper
throw new IllegalStateException("getOutputStream() called");
if (getResponse().isCommitted() || _noGzip)
+ {
+ setContentLength(_contentLength);
return getResponse().getWriter();
+ }
_gzStream=newGzipStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minGzipSize);
_writer=newWriter(_gzStream,getCharacterEncoding());
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipStream.java b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipStream.java
index cf44c479b4..aa05a1920e 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipStream.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/GzipStream.java
@@ -42,6 +42,7 @@ public class GzipStream extends ServletOutputStream
protected int _bufferSize;
protected int _minGzipSize;
protected long _contentLength;
+ protected boolean _doNotGzip;
/**
* Instantiates a new gzip stream.
@@ -77,6 +78,7 @@ public class GzipStream extends ServletOutputStream
if (_gzOut!=null)
_response.setHeader("Content-Encoding",null);
_gzOut=null;
+ _doNotGzip=false;
}
/**
@@ -87,6 +89,13 @@ public class GzipStream extends ServletOutputStream
public void setContentLength(long length)
{
_contentLength=length;
+ if (_doNotGzip && length>=0)
+ {
+ if(_contentLength<Integer.MAX_VALUE)
+ _response.setContentLength((int)_contentLength);
+ else
+ _response.setHeader("Content-Length",Long.toString(_contentLength));
+ }
}
/* ------------------------------------------------------------ */
@@ -245,14 +254,10 @@ public class GzipStream extends ServletOutputStream
throw new IllegalStateException();
if (_out==null || _bOut!=null )
{
+ _doNotGzip = true;
+
_out=_response.getOutputStream();
- if (_contentLength>=0)
- {
- if(_contentLength<Integer.MAX_VALUE)
- _response.setContentLength((int)_contentLength);
- else
- _response.setHeader("Content-Length",Long.toString(_contentLength));
- }
+ setContentLength(_contentLength);
if (_bOut!=null)
_out.write(_bOut.getBuf(),0,_bOut.getCount());
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest.java
index 9b070fff3a..feb70a449e 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest.java
@@ -14,6 +14,7 @@
package org.eclipse.jetty.servlets;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.BufferedOutputStream;
@@ -26,6 +27,7 @@ import java.util.zip.GZIPInputStream;
import javax.servlet.http.HttpServletResponse;
+import org.eclipse.jetty.http.gzip.GzipResponseWrapper;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.testing.HttpTester;
@@ -39,24 +41,37 @@ import org.junit.Test;
public class GzipFilterTest
{
- private static String __content =
- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. "+
- "Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque "+
- "habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. "+
- "Vestibulum sit amet felis augue, vel convallis dolor. Cras accumsan vehicula diam "+
- "at faucibus. Etiam in urna turpis, sed congue mi. Morbi et lorem eros. Donec vulputate "+
- "velit in risus suscipit lobortis. Aliquam id urna orci, nec sollicitudin ipsum. "+
- "Cras a orci turpis. Donec suscipit vulputate cursus. Mauris nunc tellus, fermentum "+
- "eu auctor ut, mollis at diam. Quisque porttitor ultrices metus, vitae tincidunt massa "+
- "sollicitudin a. Vivamus porttitor libero eget purus hendrerit cursus. Integer aliquam "+
- "consequat mauris quis luctus. Cras enim nibh, dignissim eu faucibus ac, mollis nec neque. "+
- "Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse "+
- "et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque.";
+ public static String __content;
+
+ static
+ {
+ // The size of content must be greater then
+ // buffer size in GzipResponseWrapper class.
+ StringBuilder builder = new StringBuilder();
+ do
+ {
+ builder.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit. In quis felis nunc. ");
+ builder.append("Quisque suscipit mauris et ante auctor ornare rhoncus lacus aliquet. Pellentesque ");
+ builder.append("habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. ");
+ builder.append("Vestibulum sit amet felis augue, vel convallis dolor. Cras accumsan vehicula diam ");
+ builder.append("at faucibus. Etiam in urna turpis, sed congue mi. Morbi et lorem eros. Donec vulputate ");
+ builder.append("velit in risus suscipit lobortis. Aliquam id urna orci, nec sollicitudin ipsum. ");
+ builder.append("Cras a orci turpis. Donec suscipit vulputate cursus. Mauris nunc tellus, fermentum ");
+ builder.append("eu auctor ut, mollis at diam. Quisque porttitor ultrices metus, vitae tincidunt massa ");
+ builder.append("sollicitudin a. Vivamus porttitor libero eget purus hendrerit cursus. Integer aliquam ");
+ builder.append("consequat mauris quis luctus. Cras enim nibh, dignissim eu faucibus ac, mollis nec neque. ");
+ builder.append("Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse ");
+ builder.append("et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque.");
+ }
+ while (builder.length() < GzipResponseWrapper.DEFAULT_BUFFER_SIZE);
+
+ __content = builder.toString();
+ }
@Rule
public TestingDir testdir = new TestingDir();
- private ServletTester tester;
+ protected ServletTester tester;
@Before
public void setUp() throws Exception
@@ -78,7 +93,7 @@ public class GzipFilterTest
tester=new ServletTester();
tester.setContextPath("/context");
tester.setResourceBase(testdir.getDir().getCanonicalPath());
- tester.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
+ tester.addServlet(getServletClass(), "/");
FilterHolder holder = tester.addFilter(GzipFilter.class,"/*",0);
holder.setInitParameter("mimeTypes","text/plain");
tester.start();
@@ -90,7 +105,12 @@ public class GzipFilterTest
tester.stop();
IO.delete(testdir.getDir());
}
-
+
+ public Class<?> getServletClass()
+ {
+ return org.eclipse.jetty.servlet.DefaultServlet.class;
+ }
+
@Test
public void testGzip() throws Exception
{
@@ -109,6 +129,7 @@ public class GzipFilterTest
response.parse(respBuff.asArray());
assertTrue(response.getMethod()==null);
+ assertNotNull("Content-Length header is missing", response.getHeader("Content-Length"));
assertTrue(response.getHeader("Content-Encoding").equalsIgnoreCase("gzip"));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
@@ -137,6 +158,7 @@ public class GzipFilterTest
response.parse(respBuff.asArray());
assertTrue(response.getMethod()==null);
+ assertNotNull("Content-Length header is missing", response.getHeader("Content-Length"));
assertEquals(__content.getBytes().length, Integer.parseInt(response.getHeader("Content-Length")));
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest1.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest1.java
new file mode 100644
index 0000000000..bb9a1e2007
--- /dev/null
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest1.java
@@ -0,0 +1,56 @@
+// ========================================================================
+// Copyright (c) 2004-2009 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.servlets;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class GzipFilterTest1 extends GzipFilterTest
+{
+ @Override
+ public Class<?> getServletClass()
+ {
+ return TestServlet.class;
+ }
+
+
+ public static class TestServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = -3603297003496724934L;
+
+ /* ------------------------------------------------------------ */
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ byte[] dataBytes = GzipFilterTest.__content.getBytes("ISO8859_1");
+
+ ServletOutputStream out = response.getOutputStream();
+
+ response.setContentLength(dataBytes.length);
+
+ String fileName = request.getServletPath();
+ if (fileName.endsWith("txt"))
+ response.setContentType("text/plain");
+ else if (fileName.endsWith("mp3"))
+ response.setContentType("audio/mpeg");
+
+ out.write(dataBytes);
+ }
+ }
+}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest2.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest2.java
new file mode 100644
index 0000000000..62d6d9cda1
--- /dev/null
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest2.java
@@ -0,0 +1,54 @@
+// ========================================================================
+// Copyright (c) 2004-2009 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.servlets;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class GzipFilterTest2 extends GzipFilterTest
+{
+ @Override
+ public Class<?> getServletClass()
+ {
+ return TestServlet.class;
+ }
+
+ public static class TestServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = -3603297003496724934L;
+
+ /* ------------------------------------------------------------ */
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ byte[] dataBytes = GzipFilterTest.__content.getBytes("ISO8859_1");
+
+ response.setContentLength(dataBytes.length);
+
+ String fileName = request.getServletPath();
+ if (fileName.endsWith("txt"))
+ response.setContentType("text/plain");
+ else if (fileName.endsWith("mp3"))
+ response.setContentType("audio/mpeg");
+
+ ServletOutputStream out = response.getOutputStream();
+ out.write(dataBytes);
+ }
+ }
+}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest3.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest3.java
new file mode 100644
index 0000000000..9fb89e0a17
--- /dev/null
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest3.java
@@ -0,0 +1,56 @@
+// ========================================================================
+// Copyright (c) 2004-2009 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.servlets;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class GzipFilterTest3 extends GzipFilterTest
+{
+ @Override
+ public Class<?> getServletClass()
+ {
+ return TestServlet.class;
+ }
+
+
+ public static class TestServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = -3603297003496724934L;
+
+ /* ------------------------------------------------------------ */
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ byte[] dataBytes = GzipFilterTest.__content.getBytes("ISO8859_1");
+
+ ServletOutputStream out = response.getOutputStream();
+
+ String fileName = request.getServletPath();
+ if (fileName.endsWith("txt"))
+ response.setContentType("text/plain");
+ else if (fileName.endsWith("mp3"))
+ response.setContentType("audio/mpeg");
+
+ response.setContentLength(dataBytes.length);
+
+ out.write(dataBytes);
+ }
+ }
+}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest4.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest4.java
new file mode 100644
index 0000000000..b64233319e
--- /dev/null
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterTest4.java
@@ -0,0 +1,54 @@
+// ========================================================================
+// Copyright (c) 2004-2009 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.servlets;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class GzipFilterTest4 extends GzipFilterTest
+{
+ @Override
+ public Class<?> getServletClass()
+ {
+ return TestServlet.class;
+ }
+
+ public static class TestServlet extends HttpServlet
+ {
+ private static final long serialVersionUID = -3603297003496724934L;
+
+ /* ------------------------------------------------------------ */
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+ {
+ byte[] dataBytes = GzipFilterTest.__content.getBytes("ISO8859_1");
+
+ String fileName = request.getServletPath();
+ if (fileName.endsWith("txt"))
+ response.setContentType("text/plain");
+ else if (fileName.endsWith("mp3"))
+ response.setContentType("audio/mpeg");
+
+ response.setContentLength(dataBytes.length);
+
+ ServletOutputStream out = response.getOutputStream();
+ out.write(dataBytes);
+ }
+ }
+}

Back to the top