Merge branch 'master' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project

This commit is contained in:
Greg Wilkins 2012-05-22 14:44:17 +02:00
commit d436400db1
5 changed files with 150 additions and 59 deletions

View File

@ -260,7 +260,7 @@ public abstract class AbstractCompressedStream extends ServletOutputStream
public void doNotCompress() throws IOException public void doNotCompress() throws IOException
{ {
if (_compressedOutputStream != null) if (_compressedOutputStream != null)
throw new IllegalStateException(); throw new IllegalStateException("Compressed output stream is already assigned.");
if (_out == null || _bOut != null) if (_out == null || _bOut != null)
{ {
_doNotCompress = true; _doNotCompress = true;

View File

@ -124,7 +124,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
public void setStatus(int sc) public void setStatus(int sc)
{ {
super.setStatus(sc); super.setStatus(sc);
if (sc<200 || sc==204 || sc==205 ||sc>=300) if (sc<200 || sc==204 || sc==205 || sc>=300)
noCompression(); noCompression();
} }
@ -344,7 +344,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
else if (_writer!=null) else if (_writer!=null)
throw new IllegalStateException("getWriter() called"); throw new IllegalStateException("getWriter() called");
return (ServletOutputStream)_compressedStream; return _compressedStream;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -366,7 +366,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
} }
_compressedStream=newCompressedStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minCompressSize); _compressedStream=newCompressedStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minCompressSize);
_writer=newWriter((OutputStream)_compressedStream,getCharacterEncoding()); _writer=newWriter(_compressedStream,getCharacterEncoding());
} }
return _writer; return _writer;
} }

View File

@ -173,6 +173,16 @@ public class ProxyServlet implements Servlet
return Log.getLogger("org.eclipse.jetty.servlets." + config.getServletName()); return Log.getLogger("org.eclipse.jetty.servlets." + config.getServletName());
} }
/**
* Create and return an HttpClientInstance
*
* @return HttpClient
*/
protected HttpClient createHttpClientInstance()
{
return new HttpClient();
}
/** /**
* Create and return an HttpClient based on ServletConfig * Create and return an HttpClient based on ServletConfig
* *
@ -185,7 +195,7 @@ public class ProxyServlet implements Servlet
*/ */
protected HttpClient createHttpClient(ServletConfig config) throws Exception protected HttpClient createHttpClient(ServletConfig config) throws Exception
{ {
HttpClient client = new HttpClient(); HttpClient client = createHttpClientInstance();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
String t = config.getInitParameter("maxThreads"); String t = config.getInitParameter("maxThreads");

View File

@ -45,11 +45,8 @@ public class GzipFilterDefaultTest
this.compressionType = compressionType; this.compressionType = compressionType;
} }
public static class HttpStatusServlet extends HttpServlet public static class HttpStatusServlet extends HttpServlet
{ {
private static final long serialVersionUID = 1L;
private int _status = 204; private int _status = 204;
public HttpStatusServlet() public HttpStatusServlet()
@ -57,11 +54,6 @@ public class GzipFilterDefaultTest
super(); super();
} }
public void setStatus (int status)
{
_status = status;
}
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{ {
@ -69,6 +61,24 @@ public class GzipFilterDefaultTest
} }
} }
public static class HttpErrorServlet extends HttpServlet
{
private int _status = 400;
public HttpErrorServlet()
{
super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.getOutputStream().write("error message".getBytes());
resp.setStatus(_status);
}
}
@Rule @Rule
public TestingDir testingdir = new TestingDir(); public TestingDir testingdir = new TestingDir();
@ -153,7 +163,28 @@ public class GzipFilterDefaultTest
try try
{ {
tester.start(); tester.start();
tester.assertIsResponseNotGzipCompressed(null, -1, 204); tester.assertIsResponseNotGzipCompressed(-1, 204);
}
finally
{
tester.stop();
}
}
@Test
public void testIsNotGzipCompressedHttpBadRequestStatus() throws Exception
{
GzipTester tester = new GzipTester(testingdir, compressionType);
// Test error code 400
FilterHolder holder = tester.setContentServlet(HttpErrorServlet.class);
holder.setInitParameter("mimeTypes","text/plain");
try
{
tester.start();
tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("error message", -1, 400);
} }
finally finally
{ {

View File

@ -14,6 +14,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.DigestOutputStream; import java.security.DigestOutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Enumeration; import java.util.Enumeration;
@ -81,7 +82,7 @@ public class GzipTester
// Assert the response headers // Assert the response headers
Assert.assertThat("Response.method",response.getMethod(),nullValue()); Assert.assertThat("Response.method",response.getMethod(),nullValue());
Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK)); // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
Assert.assertThat("Response.header[Content-Length]",response.getHeader("Content-Length"),notNullValue()); Assert.assertThat("Response.header[Content-Length]",response.getHeader("Content-Length"),notNullValue());
Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),containsString(compressionType)); Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),containsString(compressionType));
@ -222,28 +223,60 @@ public class GzipTester
*/ */
public void assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception public void assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception
{ {
System.err.printf("[GzipTester] requesting /context/%s%n",filename); String uri = "/context/"+filename;
HttpTester request = new HttpTester(); HttpTester response = executeRequest(uri);
HttpTester response = new HttpTester(); assertResponseHeaders(expectedFilesize,status,response);
request.setMethod("GET"); // Assert that the contents are what we expect.
request.setVersion("HTTP/1.0"); if (filename != null)
request.setHeader("Host","tester"); {
request.setHeader("Accept-Encoding",compressionType); File serverFile = testdir.getFile(filename);
if (this.userAgent != null) String expectedResponse = IO.readToString(serverFile);
request.setHeader("User-Agent", this.userAgent);
if (filename == null)
request.setURI("/context/");
else
request.setURI("/context/"+filename);
// Issue the request String actual = readResponse(response);
ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
// Collect the response(s) }
ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); }
response.parse(respBuff.asArray());
// Assert the response headers /**
* Asserts that the request results in a properly structured GzipFilter response, where the content is
* not compressed, and the content-length is returned appropriately.
*
* @param expectedResponse
* the expected response body string
* @param expectedFilesize
* the expected filesize to be specified on the Content-Length portion of the response headers. (note:
* passing -1 will disable the Content-Length assertion)
* @throws Exception
*/
public void assertIsResponseNotGzipCompressedAndEqualToExpectedString(String expectedResponse, int expectedFilesize, int status) throws Exception
{
String uri = "/context/";
HttpTester response = executeRequest(uri);
assertResponseHeaders(expectedFilesize,status,response);
String actual = readResponse(response);
Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
}
/**
* Asserts that the request results in a properly structured GzipFilter response, where the content is
* not compressed, and the content-length is returned appropriately.
*
* @param expectedFilesize
* the expected filesize to be specified on the Content-Length portion of the response headers. (note:
* passing -1 will disable the Content-Length assertion)
* @throws Exception
*/
public void assertIsResponseNotGzipCompressed(int expectedFilesize, int status) throws Exception
{
String uri = "/context/";
HttpTester response = executeRequest(uri);
assertResponseHeaders(expectedFilesize,status,response);
}
private void assertResponseHeaders(int expectedFilesize, int status, HttpTester response)
{
Assert.assertThat("Response.method",response.getMethod(),nullValue()); Assert.assertThat("Response.method",response.getMethod(),nullValue());
Assert.assertThat("Response.status",response.getStatus(),is(status)); Assert.assertThat("Response.status",response.getStatus(),is(status));
if (expectedFilesize != (-1)) if (expectedFilesize != (-1))
@ -253,14 +286,34 @@ public class GzipTester
Assert.assertThat("Response.header[Content-Length]",serverLength,is(expectedFilesize)); Assert.assertThat("Response.header[Content-Length]",serverLength,is(expectedFilesize));
} }
Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),not(containsString(compressionType))); Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),not(containsString(compressionType)));
}
// Assert that the contents are what we expect. private HttpTester executeRequest(String uri) throws IOException, Exception
if (filename != null)
{ {
File serverFile = testdir.getFile(filename); System.err.printf("[GzipTester] requesting %s%n",uri);
String expected = IO.readToString(serverFile); HttpTester request = new HttpTester();
String actual = null; HttpTester response = new HttpTester();
request.setMethod("GET");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("Accept-Encoding",compressionType);
if (this.userAgent != null)
request.setHeader("User-Agent", this.userAgent);
request.setURI(uri);
// Issue the request
ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes());
// Collect the response(s)
ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff);
response.parse(respBuff.asArray());
return response;
}
private String readResponse(HttpTester response) throws IOException, UnsupportedEncodingException
{
String actual = null;
InputStream in = null; InputStream in = null;
ByteArrayOutputStream out = null; ByteArrayOutputStream out = null;
try try
@ -270,17 +323,14 @@ public class GzipTester
IO.copy(in,out); IO.copy(in,out);
actual = out.toString(encoding); actual = out.toString(encoding);
Assert.assertEquals("Server contents",expected,actual);
} }
finally finally
{ {
IO.close(out); IO.close(out);
IO.close(in); IO.close(in);
} }
return actual;
} }
}
/** /**