Merged from origin/master.

This commit is contained in:
Simone Bordet 2011-12-21 10:45:06 +01:00
commit ef23bf11b8
6 changed files with 208 additions and 116 deletions

View File

@ -56,7 +56,7 @@ public class SslBytesServerTest extends SslBytesTest
private final AtomicInteger sslFlushes = new AtomicInteger();
private final AtomicInteger httpParses = new AtomicInteger();
private final AtomicReference<EndPoint> serverEndPoint = new AtomicReference<EndPoint>();
private final int idleTimeout = 5000;
private final int idleTimeout = 2000;
private ExecutorService threadPool;
private Server server;
private SSLContext sslContext;
@ -1277,7 +1277,13 @@ public class SslBytesServerTest extends SslBytesTest
}
Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
// Check client is at EOF
Assert.assertEquals(-1,client.getInputStream().read());
// Client should close the socket, but let's hold it open.
// Check that we did not spin
TimeUnit.MILLISECONDS.sleep(100);
Assert.assertThat(sslHandles.get(), lessThan(20));
Assert.assertThat(sslFlushes.get(), lessThan(20));
Assert.assertThat(httpParses.get(), lessThan(50));

View File

@ -249,6 +249,9 @@ public class SslConnection extends AbstractConnection implements AsyncConnection
try
{
LOG.debug("onIdleExpired {}ms on {}",idleForMs,this);
if (_endp.isOutputShutdown())
_sslEndPoint.close();
else
_sslEndPoint.shutdownOutput();
}
catch (IOException e)

View File

@ -30,7 +30,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
@ -1381,14 +1380,17 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
StringBuilder b = new StringBuilder();
String p = getClass().getPackage().getName();
Package pkg = getClass().getPackage();
if (pkg != null)
{
String p = pkg.getName();
if (p != null && p.length() > 0)
{
String[] ss = p.split("\\.");
for (String s : ss)
b.append(s.charAt(0)).append('.');
}
}
b.append(getClass().getSimpleName());
b.append('{').append(getContextPath()).append(',').append(getBaseResource());

View File

@ -13,25 +13,26 @@
package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.Assert;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.IO;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.matchers.JUnitMatchers.containsString;
public abstract class ConnectorTimeoutTest extends HttpServerTestFixture
{
protected static final int MAX_IDLE_TIME=250;
@ -104,6 +105,101 @@ public abstract class ConnectorTimeoutTest extends HttpServerTestFixture
Assert.assertTrue(System.currentTimeMillis()-start<5000);
}
@Test
public void testMaxIdleWithRequest10NoClientClose() throws Exception
{
configureServer(new HelloWorldHandler());
Socket client=newSocket(HOST,_connector.getLocalPort());
client.setSoTimeout(10000);
assertFalse(client.isClosed());
OutputStream os=client.getOutputStream();
InputStream is=client.getInputStream();
os.write((
"GET / HTTP/1.0\r\n"+
"host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
"connection: close\r\n"+
"\r\n").getBytes("utf-8"));
os.flush();
String result=IO.toString(is);
Assert.assertThat("OK",result,containsString("200 OK"));
assertEquals(-1, is.read());
TimeUnit.MILLISECONDS.sleep(MAX_IDLE_TIME);
// further writes will get broken pipe or similar
try
{
for (int i=0;i<100;i++)
{
os.write((
"GET / HTTP/1.0\r\n"+
"host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
"connection: keep-alive\r\n"+
"\r\n").getBytes("utf-8"));
os.flush();
}
Assert.fail("half close should have timed out");
}
catch(SocketException e)
{
// expected
}
}
@Test
public void testMaxIdleWithRequest11NoClientClose() throws Exception
{
configureServer(new EchoHandler());
Socket client=newSocket(HOST,_connector.getLocalPort());
client.setSoTimeout(10000);
assertFalse(client.isClosed());
OutputStream os=client.getOutputStream();
InputStream is=client.getInputStream();
String content="Wibble";
byte[] contentB=content.getBytes("utf-8");
os.write((
"POST /echo HTTP/1.1\r\n"+
"host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
"content-type: text/plain; charset=utf-8\r\n"+
"content-length: "+contentB.length+"\r\n"+
"connection: close\r\n"+
"\r\n").getBytes("utf-8"));
os.write(contentB);
os.flush();
IO.toString(is);
assertEquals(-1, is.read());
TimeUnit.MILLISECONDS.sleep(MAX_IDLE_TIME);
// further writes will get broken pipe or similar
try
{
for (int i=0;i<100;i++)
{
os.write((
"GET / HTTP/1.0\r\n"+
"host: "+HOST+":"+_connector.getLocalPort()+"\r\n"+
"connection: keep-alive\r\n"+
"\r\n").getBytes("utf-8"));
os.flush();
}
Assert.fail("half close should have timed out");
}
catch(SocketException e)
{
// expected
}
}
@Test
public void testMaxIdleNoRequest() throws Exception

View File

@ -332,6 +332,25 @@ public class WebSocketClient
{
if (!_factory.isStarted())
throw new IllegalStateException("Factory !started");
InetSocketAddress address = toSocketAddress(uri);
SocketChannel channel = SocketChannel.open();
if (_bindAddress != null)
channel.socket().bind(_bindAddress);
channel.socket().setTcpNoDelay(true);
WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
channel.configureBlocking(false);
channel.connect(address);
_factory.getSelectorManager().register(channel, holder);
return holder;
}
public static final InetSocketAddress toSocketAddress(URI uri)
{
String scheme = uri.getScheme();
if (!("ws".equalsIgnoreCase(scheme) || "wss".equalsIgnoreCase(scheme)))
throw new IllegalArgumentException("Bad WebSocket scheme: " + scheme);
@ -341,20 +360,8 @@ public class WebSocketClient
if (port < 0)
port = "ws".equals(scheme) ? 80 : 443;
SocketChannel channel = SocketChannel.open();
if (_bindAddress != null)
channel.socket().bind(_bindAddress);
channel.socket().setTcpNoDelay(true);
InetSocketAddress address = new InetSocketAddress(uri.getHost(), port);
WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
channel.configureBlocking(false);
channel.connect(address);
_factory.getSelectorManager().register(channel, holder);
return holder;
return address;
}
/* ------------------------------------------------------------ */
@ -486,6 +493,7 @@ public class WebSocketClient
return _maskGen;
}
@Override
public String toString()
{
return "[" + _uri + ","+_websocket+"]@"+hashCode();

View File

@ -21,6 +21,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
@ -44,6 +45,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
public class WebSocketClientTest
{
@ -711,44 +713,19 @@ public class WebSocketClientTest
@Test
public void testURIWithDefaultPort() throws Exception
{
WebSocketClient client = new WebSocketClient(_factory);
try
{
client.open(new URI("ws://localhost"), new WebSocket()
{
public void onOpen(Connection connection)
{
URI uri = new URI("ws://localhost");
InetSocketAddress addr = WebSocketClient.toSocketAddress(uri);
Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost"));
Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(80));
}
public void onClose(int closeCode, String message)
@Test
public void testURIWithDefaultWSSPort() throws Exception
{
System.out.println("closeCode = " + closeCode);
}
}).get(5, TimeUnit.SECONDS);
}
catch (ExecutionException x)
{
Assert.assertTrue(x.getCause() instanceof ConnectException);
}
try
{
client.open(new URI("wss://localhost"), new WebSocket()
{
public void onOpen(Connection connection)
{
}
public void onClose(int closeCode, String message)
{
}
}).get(5, TimeUnit.SECONDS);
}
catch (ExecutionException x)
{
Assert.assertTrue(x.getCause() instanceof ConnectException);
}
URI uri = new URI("wss://localhost");
InetSocketAddress addr = WebSocketClient.toSocketAddress(uri);
Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost"));
Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(443));
}
private void respondToClient(Socket connection, String serverResponse) throws IOException