Merge branch 'master' into session-refactor

This commit is contained in:
Jan Bartel 2015-12-03 16:59:28 +11:00
commit b0b03cfa45
10 changed files with 195 additions and 14 deletions

View File

@ -195,6 +195,8 @@ public class HttpOutput extends ServletOutputStream implements Runnable
{
return;
}
case ASYNC:
case UNREADY:
case PENDING:
{

View File

@ -29,6 +29,9 @@ import java.io.PrintWriter;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -47,6 +50,8 @@ import org.junit.Rule;
public abstract class AbstractHttpTest
{
private final static Set<String> __noBodyCodes = new HashSet<>(Arrays.asList(new String[]{"100","101","102","204","304"}));
@Rule
public TestTracker tracker = new TestTracker();
@ -93,8 +98,10 @@ public abstract class AbstractHttpTest
writer.flush();
SimpleHttpResponse response = httpParser.readResponse(reader);
if ("HTTP/1.1".equals(httpVersion) && response.getHeaders().get("content-length") == null && response
.getHeaders().get("transfer-encoding") == null)
if ("HTTP/1.1".equals(httpVersion)
&& response.getHeaders().get("content-length") == null
&& response.getHeaders().get("transfer-encoding") == null
&& !__noBodyCodes.contains(response.getCode()))
assertThat("If HTTP/1.1 response doesn't contain transfer-encoding or content-length headers, " +
"it should contain connection:close", response.getHeaders().get("connection"), is("close"));
return response;

View File

@ -30,6 +30,7 @@ import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -1536,6 +1537,71 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
}
}
@Test
public void testWriteBodyAfterNoBodyResponse() throws Exception
{
configureServer(new WriteBodyAfterNoBodyResponseHandler());
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
final OutputStream out=client.getOutputStream();
out.write("GET / HTTP/1.1\r\nHost: test\r\n\r\n".getBytes());
out.write("GET / HTTP/1.1\r\nHost: test\r\nConnection: close\r\n\r\n".getBytes());
out.flush();
BufferedReader in =new BufferedReader(new InputStreamReader(client.getInputStream()));
String line=in.readLine();
assertThat(line, containsString(" 304 "));
while (true)
{
line=in.readLine();
if (line==null)
throw new EOFException();
if (line.length()==0)
break;
assertThat(line, not(containsString("Content-Length")));
assertThat(line, not(containsString("Content-Type")));
assertThat(line, not(containsString("Transfer-Encoding")));
}
line=in.readLine();
assertThat(line, containsString(" 304 "));
while (true)
{
line=in.readLine();
if (line==null)
throw new EOFException();
if (line.length()==0)
break;
assertThat(line, not(containsString("Content-Length")));
assertThat(line, not(containsString("Content-Type")));
assertThat(line, not(containsString("Transfer-Encoding")));
}
do
{
line=in.readLine();
}
while (line!=null);
}
private class WriteBodyAfterNoBodyResponseHandler extends AbstractHandler
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
response.setStatus(304);
response.getOutputStream().print("yuck");
response.flushBuffer();
}
}
public class NoopHandler extends AbstractHandler
{
@Override

View File

@ -62,6 +62,7 @@ import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.BaseHolder.Source;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.DeprecationWarning;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.LifeCycle;
@ -90,7 +91,7 @@ public class ServletContextHandler extends ContextHandler
public interface ServletContainerInitializerCaller extends LifeCycle {};
protected final DecoratedObjectFactory _objFactory = new DecoratedObjectFactory();
protected final DecoratedObjectFactory _objFactory;
protected Class<? extends SecurityHandler> _defaultSecurityHandlerClass=org.eclipse.jetty.security.ConstraintSecurityHandler.class;
protected SessionHandler _sessionHandler;
protected SecurityHandler _securityHandler;
@ -150,6 +151,9 @@ public class ServletContextHandler extends ContextHandler
_sessionHandler = sessionHandler;
_securityHandler = securityHandler;
_servletHandler = servletHandler;
_objFactory = new DecoratedObjectFactory();
_objFactory.addDecorator(new DeprecationWarning());
if (contextPath!=null)
setContextPath(contextPath);

View File

@ -658,16 +658,23 @@ public class AsyncServletIOTest
assertTrue(chunked);
// Get body slowly
String last;
while (true)
String last=null;
try
{
last=line;
//Thread.sleep(1000);
line = in.readLine();
LOG.debug("body: "+line);
if (line==null)
break;
list.add(line);
while (true)
{
last=line;
//Thread.sleep(1000);
line = in.readLine();
LOG.debug("body: "+line);
if (line==null)
break;
list.add(line);
}
}
catch(IOException e)
{
}
LOG.debug("last: "+last);

View File

@ -387,7 +387,7 @@ public class ServletContextHandlerTest
String expected = String.format("Attribute[%s] = %s", DecoratedObjectFactory.ATTR, DecoratedObjectFactory.class.getName());
assertThat("Has context attribute", response, containsString(expected));
assertThat("Decorators size", response, containsString("Decorators.size = [1]"));
assertThat("Decorators size", response, containsString("Decorators.size = [2]"));
expected = String.format("decorator[] = %s", DummyLegacyDecorator.class.getName());
assertThat("Specific Legacy Decorator", response, containsString(expected));
@ -414,7 +414,7 @@ public class ServletContextHandlerTest
String expected = String.format("Attribute[%s] = %s", DecoratedObjectFactory.ATTR, DecoratedObjectFactory.class.getName());
assertThat("Has context attribute", response, containsString(expected));
assertThat("Decorators size", response, containsString("Decorators.size = [1]"));
assertThat("Decorators size", response, containsString("Decorators.size = [2]"));
expected = String.format("decorator[] = %s", DummyUtilDecorator.class.getName());
assertThat("Specific Legacy Decorator", response, containsString(expected));

View File

@ -27,6 +27,9 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/**
*/
@ -34,9 +37,12 @@ import javax.servlet.ServletResponse;
@Deprecated
public class GzipFilter implements Filter
{
private static final Logger LOG = Log.getLogger(GzipFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
LOG.warn("GzipFilter is deprecated. Use GzipHandler");
}
@Override

View File

@ -0,0 +1,86 @@
//
// ========================================================================
// Copyright (c) 1995-2015 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.util;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class DeprecationWarning implements Decorator
{
private static final Logger LOG = Log.getLogger(DeprecationWarning.class);
@Override
public <T> T decorate(T o)
{
if (o == null)
{
return null;
}
Class<?> clazz = o.getClass();
try
{
Deprecated depr = clazz.getAnnotation(Deprecated.class);
if (depr != null)
{
LOG.warn("Using @Deprecated Class {}",clazz.getName());
}
}
catch (Throwable t)
{
LOG.ignore(t);
}
verifyIndirectTypes(clazz.getSuperclass(),clazz,"Class");
for (Class<?> ifaceClazz : clazz.getInterfaces())
{
verifyIndirectTypes(ifaceClazz,clazz,"Interface");
}
return o;
}
private void verifyIndirectTypes(Class<?> superClazz, Class<?> clazz, String typeName)
{
try
{
// Report on super class deprecation too
while (superClazz != null && superClazz != Object.class)
{
Deprecated supDepr = superClazz.getAnnotation(Deprecated.class);
if (supDepr != null)
{
LOG.warn("Using indirect @Deprecated {} {} - (seen from {})",typeName,superClazz.getName(),clazz);
}
superClazz = superClazz.getSuperclass();
}
}
catch (Throwable t)
{
LOG.ignore(t);
}
}
@Override
public void destroy(Object o)
{
}
}

View File

@ -109,6 +109,7 @@ public class DecoratorsLegacyTest
}
}
@SuppressWarnings("deprecation")
private static class DummyLegacyDecorator implements org.eclipse.jetty.servlet.ServletContextHandler.Decorator
{
@Override
@ -136,6 +137,7 @@ public class DecoratorsLegacyTest
@Override
protected void configureServletContextHandler(ServletContextHandler context)
{
context.getObjectFactory().clear();
// Add decorator in the legacy way
context.addDecorator(new DummyLegacyDecorator());
}

View File

@ -136,6 +136,7 @@ public class DecoratorsTest
protected void configureServletContextHandler(ServletContextHandler context)
{
// Add decorator in the new util way
context.getObjectFactory().clear();
context.getObjectFactory().addDecorator(new DummyUtilDecorator());
}
};