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

This commit is contained in:
Jan Bartel 2013-07-25 11:26:35 +10:00
commit 72b077921d
6 changed files with 79 additions and 7 deletions

View File

@ -125,10 +125,10 @@ public class HttpExchange
protected void expire(HttpDestination destination)
{
AbstractHttpConnection connection = _connection;
if (getStatus() < HttpExchange.STATUS_COMPLETED)
setStatus(HttpExchange.STATUS_EXPIRED);
destination.exchangeExpired(this);
AbstractHttpConnection connection = _connection;
if (connection != null)
connection.exchangeExpired(this);
}

View File

@ -18,13 +18,28 @@
package org.eclipse.jetty.client;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* IdleTimeoutTest
*
@ -77,4 +92,56 @@ public class IdleTimeoutTest
Assert.fail("Test did not complete in time");
}
@Test
public void testConnectionsAreReleasedWhenExpired() throws Exception
{
// we need a server that times out and a client with shorter timeout settings, so we need to create new ones
Server server = new Server();
Connector connector = new SelectChannelConnector();
server.addConnector(connector);
server.setHandler(new AbstractHandler()
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (request.getParameter("timeout") != null)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
baseRequest.setHandled(true);
response.getWriter().write("Hello world");
}
});
server.start();
HttpClient httpClient = new HttpClient();
httpClient.setMaxConnectionsPerAddress(1);
httpClient.setConnectTimeout(200);
httpClient.setTimeout(200);
httpClient.setIdleTimeout(200);
httpClient.start();
String uriString = "http://localhost:" + connector.getLocalPort() + "/";
HttpExchange httpExchange = new HttpExchange();
httpExchange.setURI(URI.create(uriString).resolve("?timeout=true"));
httpExchange.setMethod(HttpMethods.GET);
httpClient.send(httpExchange);
int status = httpExchange.waitForDone();
assertThat("First request expired", status, is(8));
httpExchange = new HttpExchange();
httpExchange.setURI(URI.create(uriString));
httpExchange.setMethod(HttpMethods.GET);
httpClient.send(httpExchange);
status = httpExchange.waitForDone();
assertThat("Second request was successful as timeout is not set", status, is(7));
}
}

View File

@ -1015,14 +1015,17 @@ public class HttpGenerator extends AbstractGenerator
// If we need EOC and everything written
if (_needEOC && (_content == null || _content.length() == 0))
{
if (_header == null && _buffer == null)
_header = _buffers.getHeader();
if (_needCRLF)
{
if (_buffer == null && _header != null && _header.space() >= 2)
if (_buffer == null && _header != null && _header.space() >= HttpTokens.CRLF.length)
{
_header.put(HttpTokens.CRLF);
_needCRLF = false;
}
else if (_buffer!=null && _buffer.space() >= 2)
else if (_buffer!=null && _buffer.space() >= HttpTokens.CRLF.length)
{
_buffer.put(HttpTokens.CRLF);
_needCRLF = false;

View File

@ -2506,7 +2506,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
if (dot<0)
return false;
String suffix=path.substring(dot);
return resource.getAlias().toString().endsWith(suffix);
return resource.toString().endsWith(suffix);
}
}
@ -2521,10 +2521,10 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
public boolean check(String path, Resource resource)
{
int slash = path.lastIndexOf('/');
if (slash<0)
if (slash<0 || slash==path.length()-1)
return false;
String suffix=path.substring(slash);
return resource.getAlias().toString().endsWith(suffix);
return resource.toString().endsWith(suffix);
}
}
/* ------------------------------------------------------------ */
@ -2537,7 +2537,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
public boolean check(String path, Resource resource)
{
int slash = path.lastIndexOf('/');
if (slash<0)
if (slash<0 || resource.exists())
return false;
String suffix=path.substring(slash);
return resource.getAlias().toString().endsWith(suffix);

View File

@ -67,6 +67,7 @@ public class JspAndDefaultWithAliasesTest
// @formatter:off
data.add(new String[] { "false","/dump.jsp" });
data.add(new String[] { "false","/dump.jsp/" });
data.add(new String[] { "true", "/dump.jsp%00" });
data.add(new String[] { "false","/dump.jsp%00/" });
data.add(new String[] { "false","/dump.jsp%00x/dump.jsp" });

View File

@ -68,6 +68,7 @@ public class JspAndDefaultWithoutAliasesTest
// @formatter:off
data.add(new Object[] { "/dump.jsp" });
data.add(new Object[] { "/dump.jsp/" });
data.add(new Object[] { "/dump.jsp%00" });
data.add(new Object[] { "/dump.jsp%00x" });
data.add(new Object[] { "/dump.jsp%00x/dump.jsp" });