Merge remote-tracking branch 'origin/jetty-7' into jetty-8

Conflicts:
	tests/test-integration/src/test/java/org/eclipse/jetty/test/jsp/JspAndDefaultWithoutAliasesTest.java
This commit is contained in:
Greg Wilkins 2013-01-10 12:06:55 +11:00
commit 74edf1867c
8 changed files with 281 additions and 33 deletions

View File

@ -429,44 +429,37 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
boolean endsWithSlash=(pathInfo==null?request.getServletPath():pathInfo).endsWith(URIUtil.SLASH);
// Can we gzip this request?
String pathInContextGz=null;
boolean gzip=false;
if (!included.booleanValue() && _gzip && reqRanges==null && !endsWithSlash )
{
// Tell caches that response may vary by accept-encoding
response.setHeader(HttpHeaders.VARY,HttpHeaders.ACCEPT_ENCODING);
// Should we vary this response according to accept-encoding?
String accept=request.getHeader(HttpHeaders.ACCEPT_ENCODING);
if (accept!=null && accept.indexOf("gzip")>=0)
gzip=true;
}
// Find the resource and content
Resource resource=null;
HttpContent content=null;
try
{
// Try gzipped content first
if (gzip)
// is gzip enabled?
String pathInContextGz=null;
boolean gzip=false;
if (!included.booleanValue() && _gzip && reqRanges==null && !endsWithSlash )
{
// Look for a gzip resource
pathInContextGz=pathInContext+".gz";
if (_cache==null)
{
resource=getResource(pathInContextGz);
}
else
{
content=_cache.lookup(pathInContextGz);
resource=(content==null)?null:content.getResource();
}
if (resource==null || !resource.exists() || resource.isDirectory())
// Does a gzip resource exist?
if (resource!=null && resource.exists() && !resource.isDirectory())
{
gzip=false;
pathInContextGz=null;
// Tell caches that response may vary by accept-encoding
response.setHeader(HttpHeaders.VARY,HttpHeaders.ACCEPT_ENCODING);
// Does the client accept gzip?
String accept=request.getHeader(HttpHeaders.ACCEPT_ENCODING);
if (accept!=null && accept.indexOf("gzip")>=0)
gzip=true;
}
}

View File

@ -641,6 +641,42 @@ public class DefaultServletTest
}
@Test
public void testGzip() throws Exception
{
testdir.ensureEmpty();
File resBase = testdir.getFile("docroot");
FS.ensureDirExists(resBase);
File file0 = new File(resBase, "data0.txt");
createFile(file0, "Hello Text 0");
File file0gz = new File(resBase, "data0.txt.gz");
createFile(file0gz, "fake gzip");
String resBasePath = resBase.getAbsolutePath();
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "true");
defholder.setInitParameter("resourceBase", resBasePath);
String response = connector.getResponses("GET /context/data0.txt HTTP/1.1\r\nHost:localhost:8080\r\n\r\n");
assertResponseContains("Content-Length: 12", response);
assertResponseContains("Hello Text 0",response);
assertResponseContains("Vary: Accept-Encoding",response);
assertResponseNotContains("Content-Encoding: gzip",response);
response = connector.getResponses("GET /context/data0.txt HTTP/1.1\r\nHost:localhost:8080\r\nAccept-Encoding:gzip\r\n\r\n");
assertResponseContains("Content-Length: 9", response);
assertResponseContains("fake gzip",response);
assertResponseContains("Vary: Accept-Encoding",response);
assertResponseContains("Content-Encoding: gzip",response);
}
@Test
public void testIfModifiedSmall() throws Exception
{

View File

@ -30,6 +30,7 @@ import java.util.zip.GZIPOutputStream;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@ -111,6 +112,7 @@ public class GzipFilter extends UserAgentFilter
public final static String ETAG_DEFLATE="-deflate\"";
public final static String ETAG="o.e.j.s.GzipFilter.ETag";
protected ServletContext _context;
protected Set<String> _mimeTypes;
protected int _bufferSize=8192;
protected int _minGzipSize=256;
@ -136,6 +138,8 @@ public class GzipFilter extends UserAgentFilter
{
super.init(filterConfig);
_context=filterConfig.getServletContext();
String tmp=filterConfig.getInitParameter("bufferSize");
if (tmp!=null)
_bufferSize=Integer.parseInt(tmp);
@ -217,6 +221,20 @@ public class GzipFilter extends UserAgentFilter
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
// Check if mime type of request can ever be compressed.
if (_mimeTypes!=null && _mimeTypes.size()>0)
{
String mimeType = _context.getMimeType(request.getRequestURI());
if (mimeType!=null && !_mimeTypes.contains(mimeType))
{
// handle normally without setting vary header
super.doFilter(request,response,chain);
return;
}
}
// Inform caches that responses may vary according to Accept-Encoding
response.setHeader("Vary","Accept-Encoding");

View File

@ -27,11 +27,14 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.Assert;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.gzip.CompressedResponseWrapper;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlets.gzip.GzipTester;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.junit.Rule;
import org.junit.Test;
@ -115,7 +118,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseGzipCompressed("file.txt");
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
}
finally
{
@ -138,7 +142,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseGzipCompressed("file.txt");
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
}
finally
{
@ -161,7 +166,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseGzipCompressed("file.txt");
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
}
finally
{
@ -184,7 +190,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseGzipCompressed("file.txt");
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
}
finally
{
@ -206,7 +213,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("file.txt", filesize, HttpStatus.OK_200);
HttpTester http = tester.assertIsResponseNotGzipCompressed("file.txt", filesize, HttpStatus.OK_200);
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
}
finally
{
@ -219,7 +227,6 @@ public class GzipFilterDefaultTest
{
GzipTester tester = new GzipTester(testingdir, compressionType);
// Test content that is smaller than the buffer.
int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 4;
tester.prepareServerFile("file.mp3",filesize);
@ -229,7 +236,8 @@ public class GzipFilterDefaultTest
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("file.mp3", filesize, HttpStatus.OK_200);
HttpTester http = tester.assertIsResponseNotGzipCompressed("file.mp3", filesize, HttpStatus.OK_200);
Assert.assertNull(http.getHeader("Vary"));
}
finally
{

View File

@ -99,7 +99,7 @@ public class IncludableGzipFilterMinSizeTest
tester.setGzipFilterClass(IncludableGzipFilter.class);
FilterHolder holder = tester.setContentServlet(testServlet);
holder.setInitParameter("mimeTypes","application/soap+xml,text/javascript");
holder.setInitParameter("mimeTypes","application/soap+xml,text/javascript,application/x-javascript");
holder.setInitParameter("minGzipSize", "2048");
holder.setInitParameter("uncheckedPrintWriter","true");

View File

@ -75,12 +75,12 @@ public class GzipTester
// DOES NOT WORK IN WINDOWS - this.testdir.ensureEmpty();
}
public void assertIsResponseGzipCompressed(String filename) throws Exception
public HttpTester assertIsResponseGzipCompressed(String filename) throws Exception
{
assertIsResponseGzipCompressed(filename,filename);
return assertIsResponseGzipCompressed(filename,filename);
}
public void assertIsResponseGzipCompressed(String requestedFilename, String serverFilename) throws Exception
public HttpTester assertIsResponseGzipCompressed(String requestedFilename, String serverFilename) throws Exception
{
System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename);
HttpTester request = new HttpTester();
@ -141,6 +141,8 @@ public class GzipTester
IO.close(in);
IO.close(bais);
}
return response;
}
/**
@ -245,7 +247,7 @@ public class GzipTester
* passing -1 will disable the Content-Length assertion)
* @throws Exception
*/
public void assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception
public HttpTester assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception
{
String uri = "/context/"+filename;
HttpTester response = executeRequest(uri);
@ -260,6 +262,8 @@ public class GzipTester
String actual = readResponse(response);
Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
}
return response;
}

View File

@ -0,0 +1,189 @@
//
// ========================================================================
// Copyright (c) 1995-2012 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.test.jsp;
import static org.hamcrest.Matchers.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.jasper.servlet.JspServlet;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* Test various paths for JSP resources that tickle various java.io.File bugs to get around the JspServlet matching, that then flows to the DefaultServlet to be
* served as source files.
*/
@Ignore("Disabled till greg can look at it")
@RunWith(Parameterized.class)
public class JspAndDefaultWithAliasesTest
{
private static final Logger LOG = Log.getLogger(JspAndDefaultWithAliasesTest.class);
private static Server server;
private static URI serverURI;
@Parameters
public static Collection<String[]> data()
{
List<String[]> data = new ArrayList<String[]>();
// @formatter:off
data.add(new String[] { "/dump.jsp" });
data.add(new String[] { "/dump.jsp%00" });
data.add(new String[] { "/dump.jsp%00x" });
data.add(new String[] { "/dump.jsp%00/" });
data.add(new String[] { "/dump.jsp%00x/" });
data.add(new String[] { "/dump.jsp%00x/dump.jsp" });
data.add(new String[] { "/dump.jsp%00/dump.jsp" });
data.add(new String[] { "/dump.jsp%00/index.html" });
// @formatter:on
return data;
}
@BeforeClass
public static void startServer() throws Exception
{
server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(0);
server.addConnector(connector);
// Configure LoginService
HashLoginService login = new HashLoginService();
login.setName("Test Realm");
File realmFile = MavenTestingUtils.getTestResourceFile("realm.properties");
login.setConfig(realmFile.getAbsolutePath());
server.addBean(login);
// Configure WebApp
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
File webappBase = MavenTestingUtils.getTestResourceDir("docroots/jsp");
context.setResourceBase(webappBase.getAbsolutePath());
context.setClassLoader(Thread.currentThread().getContextClassLoader());
// add default servlet
ServletHolder defaultServHolder = context.addServlet(DefaultServlet.class,"/");
defaultServHolder.setInitParameter("aliases","true"); // important! must be TRUE
// add jsp
ServletHolder jsp = context.addServlet(JspServlet.class,"*.jsp");
jsp.setInitParameter("classpath",context.getClassPath());
// add context
server.setHandler(context);
server.start();
serverURI = new URI("http://localhost:" + connector.getLocalPort() + "/");
}
@AfterClass
public static void stopServer() throws Exception
{
server.stop();
}
private String path;
public JspAndDefaultWithAliasesTest(String encodedRequestPath)
{
LOG.info("Path \"" + encodedRequestPath + "\"");
this.path = encodedRequestPath;
}
private void assertProcessedByJspServlet(HttpURLConnection conn) throws IOException
{
// make sure that jsp actually ran, and didn't just get passed onto
// the default servlet to return the jsp source
String body = getResponseBody(conn);
Assert.assertThat("Body",body,not(containsString("<%@")));
Assert.assertThat("Body",body,not(containsString("<jsp:")));
}
private void assertResponse(HttpURLConnection conn) throws IOException
{
if (conn.getResponseCode() == 200)
{
// Serving content is allowed, but it better be the processed JspServlet
assertProcessedByJspServlet(conn);
return;
}
// Of other possible paths, only 404 Not Found is expected
Assert.assertThat("Response Code",conn.getResponseCode(),is(404));
}
@Test
public void testGetReference() throws Exception
{
URI uri = serverURI.resolve(path);
HttpURLConnection conn = null;
try
{
conn = (HttpURLConnection)uri.toURL().openConnection();
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
assertResponse(conn);
}
finally
{
conn.disconnect();
}
}
protected String getResponseBody(HttpURLConnection conn) throws IOException
{
InputStream in = null;
try
{
in = conn.getInputStream();
return IO.toString(in);
}
finally
{
IO.close(in);
}
}
}