jetty-9 further work on oneconnector... passing more tests

This commit is contained in:
Greg Wilkins 2012-08-02 08:14:02 +10:00
parent 2a470631bf
commit 56a2cedaa5
13 changed files with 111 additions and 276 deletions

View File

@ -295,10 +295,6 @@ public abstract class AbstractConnector extends AggregateLifeCycle implements Co
if (thread != null)
thread.interrupt();
}
int i = _name.lastIndexOf("/");
if (i > 0)
_name = _name.substring(0, i);
}
public void join() throws InterruptedException

View File

@ -99,6 +99,10 @@ public abstract class AbstractNetConnector extends AbstractConnector implements
LOG.warn(e);
}
super.doStop();
int i = getName().lastIndexOf("/");
if (i > 0)
setName(getName().substring(0, i));
}
@Override

View File

@ -128,7 +128,7 @@ public class HttpConfiguration extends AggregateLifeCycle
if (isSecure())
{
request.setScheme(HttpScheme.HTTPS.asString());
SslConnection.SslEndPoint ssl_endp = (SslConnection.SslEndPoint)request.getHttpChannel().getEndPoint();
SslConnection.DecryptedEndPoint ssl_endp = (SslConnection.DecryptedEndPoint)request.getHttpChannel().getEndPoint();
SslConnection sslConnection = ssl_endp.getSslConnection();
SSLEngine sslEngine=sslConnection.getSSLEngine();
SslCertificates.customize(sslEngine,request);

View File

@ -17,9 +17,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.io.AsyncByteArrayEndPoint;
@ -35,13 +33,9 @@ public class LocalConnector extends AbstractConnector
private final BlockingQueue<LocalEndPoint> _connects = new LinkedBlockingQueue<>();
// TODO this sux
private final LocalExecutor _executor;
public LocalConnector(Server server)
{
super(server,new LocalExecutor(server.getThreadPool()),null,null,null, false,-1);
_executor=(LocalExecutor)getExecutor();
super(server,null,null,null,null, false,-1);
setIdleTimeout(30000);
}
@ -64,6 +58,19 @@ public class LocalConnector extends AbstractConnector
return result==null?null:BufferUtil.toString(result,StringUtil.__UTF8_CHARSET);
}
/** Sends requests and get responses based on thread activity.
* Returns all the responses received once the thread activity has
* returned to the level it was before the requests.
* @param requests the requests
* @return the responses
* @throws Exception if the requests fail
*/
public String getResponses(String requests,long idleFor,TimeUnit units) throws Exception
{
ByteBuffer result = getResponses(BufferUtil.toBuffer(requests,StringUtil.__UTF8_CHARSET),idleFor,units);
return result==null?null:BufferUtil.toString(result,StringUtil.__UTF8_CHARSET);
}
/** Sends requests and get's responses based on thread activity.
* Returns all the responses received once the thread activity has
* returned to the level it was before the requests.
@ -72,15 +79,25 @@ public class LocalConnector extends AbstractConnector
* @throws Exception if the requests fail
*/
public ByteBuffer getResponses(ByteBuffer requestsBuffer) throws Exception
{
return getResponses(requestsBuffer,100,TimeUnit.MILLISECONDS);
}
/** Sends requests and get's responses based on thread activity.
* Returns all the responses received once the thread activity has
* returned to the level it was before the requests.
* @param requestsBuffer the requests
* @return the responses
* @throws Exception if the requests fail
*/
public ByteBuffer getResponses(ByteBuffer requestsBuffer,long idleFor,TimeUnit units) throws Exception
{
LOG.debug("getResponses");
Phaser phaser=_executor._phaser;
int phase = phaser.register(); // the corresponding arrival will be done by the acceptor thread when it takes
LocalEndPoint request = new LocalEndPoint();
request.setInput(requestsBuffer);
_connects.add(request);
phaser.awaitAdvance(phase);
return request.takeOutput();
LocalEndPoint endp = new LocalEndPoint();
endp.setInput(requestsBuffer);
_connects.add(endp);
endp.waitUntilClosedOrIdleFor(idleFor,units);
return endp.takeOutput();
}
/**
@ -91,8 +108,6 @@ public class LocalConnector extends AbstractConnector
*/
public LocalEndPoint executeRequest(String rawRequest)
{
Phaser phaser=_executor._phaser;
phaser.register(); // the corresponding arrival will be done by the acceptor thread when it takes
LocalEndPoint endp = new LocalEndPoint();
endp.setInput(BufferUtil.toBuffer(rawRequest,StringUtil.__UTF8_CHARSET));
_connects.add(endp);
@ -109,48 +124,8 @@ public class LocalConnector extends AbstractConnector
endp.onOpen();
connection.onOpen();
connectionOpened(connection);
_executor._phaser.arriveAndDeregister(); // arrive for the register done in getResponses
}
private static class LocalExecutor implements Executor
{
private final Phaser _phaser=new Phaser()
{
@Override
protected boolean onAdvance(int phase, int registeredParties)
{
return false;
}
};
private final Executor _executor;
private LocalExecutor(Executor e)
{
_executor=e;
}
@Override
public void execute(final Runnable task)
{
_phaser.register();
LOG.debug("{} execute {} {}",this,task,_phaser);
_executor.execute(new Runnable()
{
@Override
public void run()
{
try
{
task.run();
}
finally
{
_phaser.arriveAndDeregister();
}
}
});
}
}
public class LocalEndPoint extends AsyncByteArrayEndPoint
{
@ -178,6 +153,7 @@ public class LocalConnector extends AbstractConnector
if (was_open)
{
connectionClosed(getAsyncConnection());
getAsyncConnection().onClose();
onClose();
}
}
@ -216,5 +192,28 @@ public class LocalConnector extends AbstractConnector
}
}
}
public void waitUntilClosedOrIdleFor(long idleFor,TimeUnit units)
{
int size=getOutput().remaining();
while (isOpen())
{
try
{
if (!_closed.await(idleFor,units))
{
if (size==getOutput().remaining())
return;
size=getOutput().remaining();
}
}
catch(Exception e)
{
LOG.warn(e);
}
}
}
}
}

View File

@ -40,11 +40,7 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
public class SslSelectChannelConnector extends SelectChannelConnector
{
public SslSelectChannelConnector(Server server)
SslConnection.DecryptedEndPoint ssl_endp = (SslConnection.DecryptedEndPoint)request.getHttpChannel().getEndPoint();
getSelectorManager().connectionOpened(delegate);
{
super(server,true);
}
}

View File

@ -31,6 +31,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.toolchain.test.Stress;
import org.eclipse.jetty.util.IO;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class HttpServerTestFixture
@ -52,9 +53,14 @@ public class HttpServerTestFixture
return socket;
}
protected static void startServer(Connector.NetConnector connector) throws Exception
@BeforeClass
public static void before()
{
_server = new Server();
}
protected static void startServer(Connector.NetConnector connector) throws Exception
{
_connector = connector;
_server.addConnector(_connector);
_server.setHandler(new HandlerWrapper());

View File

@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
@ -80,6 +81,7 @@ public class RequestTest
{
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
Map map = null;
@ -122,6 +124,7 @@ public class RequestTest
{
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
try
@ -167,6 +170,7 @@ public class RequestTest
{
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
String value=request.getParameter("param");
@ -213,6 +217,7 @@ public class RequestTest
final ArrayList<String> results = new ArrayList<String>();
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
results.add(request.getContentType());
@ -263,6 +268,7 @@ public class RequestTest
final ArrayList<String> results = new ArrayList<String>();
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
results.add(request.getRemoteAddr());
@ -305,10 +311,11 @@ public class RequestTest
"GET / HTTP/1.1\n"+
"Host: [::1]:8888\n"+
"Connection: close\n"+
"x-forwarded-for: remote\n"+
"x-forwarded-proto: https\n"+
"\n"
);
"\n",10,TimeUnit.SECONDS);
int i=0;
assertEquals("0.0.0.0",results.get(i++));
@ -345,6 +352,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
//assertEquals(request.getContentLength(), ((Request)request).getContentRead());
@ -377,6 +385,7 @@ public class RequestTest
{
Handler handler = new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
@ -422,6 +431,7 @@ public class RequestTest
{
Handler handler = new AbstractHandler()
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
@ -506,6 +516,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException
{
response.getOutputStream().println("Hello World");
@ -573,6 +584,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException
{
response.setHeader("Connection","TE");
@ -609,6 +621,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException
{
javax.servlet.http.Cookie[] ca = request.getCookies();
@ -759,6 +772,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
for (int i=0;i<cookie.length; i++)
@ -851,6 +865,7 @@ public class RequestTest
_handler._checker = new RequestTester()
{
@Override
public boolean check(HttpServletRequest request,HttpServletResponse response)
{
return "b".equals(request.getParameter("a")) && request.getParameter("c")==null;
@ -883,6 +898,7 @@ public class RequestTest
private RequestTester _checker;
private String _content;
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
((Request)request).setHandled(true);

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.server;
import org.junit.After;
import org.junit.Before;
@ -27,4 +28,10 @@ public class SelectChannelConnectorCloseTest extends ConnectorCloseTestBase
System.setProperty("org.eclipse.jetty.util.log.DEBUG","true");
startServer(new SelectChannelConnector(_server));
}
@After
public void after() throws Exception
{
_server.stop();
}
}

View File

@ -39,6 +39,7 @@ import junit.framework.TestCase;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
@ -77,7 +78,7 @@ public class SSLCloseTest extends TestCase
public void testClose() throws Exception
{
Server server=new Server();
SslSelectChannelConnector connector=new SslSelectChannelConnector(server);
SelectChannelConnector connector=new SelectChannelConnector(server,true);
String keystore = System.getProperty("user.dir")+File.separator+"src"+File.separator+"test"+File.separator+"resources"+File.separator+"keystore";
@ -98,11 +99,13 @@ public class SSLCloseTest extends TestCase
int port=connector.getLocalPort();
// System.err.println("write:"+i);
Socket socket=ctx.getSocketFactory().createSocket("localhost",port);
OutputStream os=socket.getOutputStream();
os.write("GET /test HTTP/1.1\r\nHost:test\r\nConnection:close\r\n\r\n".getBytes());
os.write((
"GET /test HTTP/1.1\r\n"+
"Host:test\r\n"+
"Connection:close\r\n\r\n").getBytes());
os.flush();
BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
@ -110,18 +113,14 @@ public class SSLCloseTest extends TestCase
String line;
while ((line=in.readLine())!=null)
{
// System.err.println(line);
if (line.trim().length()==0)
break;
}
Thread.sleep(2000);
// System.err.println(__endp);
while ((line=in.readLine())!=null)
//System.err.println(line);
Thread.yield();
}

View File

@ -52,6 +52,7 @@ import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
/**
@ -108,7 +109,6 @@ public class SSLEngineTest
server.stop();
server.join();
}
@Test
public void testBigResponse() throws Exception

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.concurrent.Callable;
@ -45,8 +46,11 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.io.AsyncConnection;
import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.SelectChannelEndPoint;
import org.eclipse.jetty.io.SelectorManager.ManagedSelector;
import org.eclipse.jetty.io.ssl.SslConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
@ -73,7 +77,6 @@ public class SslBytesServerTest extends SslBytesTest
private int serverPort;
private SSLContext sslContext;
private SimpleProxy proxy;
private Runnable idleHook;
@Before
public void init() throws Exception
@ -81,69 +84,19 @@ public class SslBytesServerTest extends SslBytesTest
threadPool = Executors.newCachedThreadPool();
server = new Server();
SslSelectChannelConnector connector = new SslSelectChannelConnector(server)
SelectChannelConnector connector = new SelectChannelConnector(server,true)
{
@Override
protected SslConnection newSslConnection(AsyncEndPoint endPoint, SSLEngine engine)
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
{
serverEndPoint.set(endPoint);
return super.newSslConnection(endPoint, engine);
// return new SslConnection(engine, endPoint)
// {
// @Override
// public Connection handle() throws IOException
// {
// sslHandles.incrementAndGet();
// return super.handle();
// }
//
// @Override
// protected SslEndPoint newSslEndPoint()
// {
// return new SslEndPoint()
// {
// @Override
// public int flush(ByteBuffer buffer) throws IOException
// {
// sslFlushes.incrementAndGet();
// return super.flush(buffer);
// }
// };
// }
//
// @Override
// public void onIdleExpired(long idleForMs)
// {
// final Runnable idleHook = SslBytesServerTest.this.idleHook;
// if (idleHook != null)
// idleHook.run();
// super.onIdleExpired(idleForMs);
// }
// };
}
@Override
protected AsyncConnection newPlainConnection(SocketChannel channel, AsyncEndPoint endPoint)
{
return super.newPlainConnection(channel, endPoint);
// return new HttpConnection(this, endPoint, getServer())
// {
// @Override
// protected HttpParser newHttpParser(Buffers requestBuffers, EndPoint endPoint, HttpParser.EventHandler requestHandler)
// {
// return new HttpParser(requestBuffers, endPoint, requestHandler)
// {
// @Override
// public int parseNext() throws IOException
// {
// httpParses.incrementAndGet();
// return super.parseNext();
// }
// };
// }
// };
SelectChannelEndPoint endp = super.newEndPoint(channel,selectSet,key);
serverEndPoint.set(endp);
return endp;
}
};
connector.setIdleTimeout(idleTimeout);
// connector.setPort(5870);
@ -1573,33 +1526,8 @@ public class SslBytesServerTest extends SslBytesTest
public void testRequestConcurrentWithIdleExpiration() throws Exception
{
final SSLSocket client = newClient();
final OutputStream clientOutput = client.getOutputStream();
final CountDownLatch latch = new CountDownLatch(1);
idleHook = new Runnable()
{
public void run()
{
if (latch.getCount() == 0)
return;
try
{
// Send request
clientOutput.write(("" +
"GET / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"\r\n").getBytes("UTF-8"));
clientOutput.flush();
latch.countDown();
}
catch (Exception x)
{
// Latch won't trigger and test will fail
x.printStackTrace();
}
}
};
SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
client.startHandshake();
Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));

View File

@ -428,13 +428,6 @@ public class SslContextFactory extends AbstractLifeCycle
return _keyStorePath;
}
/* ------------------------------------------------------------ */
@Deprecated
public String getKeyStore()
{
return _keyStorePath;
}
/* ------------------------------------------------------------ */
/**
* @param keyStorePath
@ -447,19 +440,6 @@ public class SslContextFactory extends AbstractLifeCycle
_keyStorePath = keyStorePath;
}
/* ------------------------------------------------------------ */
/**
* @param keyStorePath the file system path or URL of the keystore
* @deprecated Use {@link #setKeyStorePath(String)}
*/
@Deprecated
public void setKeyStore(String keyStorePath)
{
checkNotStarted();
_keyStorePath = keyStorePath;
}
/* ------------------------------------------------------------ */
/**
* @return The provider of the key store
@ -502,34 +482,6 @@ public class SslContextFactory extends AbstractLifeCycle
_keyStoreType = keyStoreType;
}
/* ------------------------------------------------------------ */
/** Get the _keyStoreInputStream.
* @return the _keyStoreInputStream
*
* @deprecated
*/
@Deprecated
public InputStream getKeyStoreInputStream()
{
checkKeyStore();
return _keyStoreInputStream;
}
/* ------------------------------------------------------------ */
/** Set the keyStoreInputStream.
* @param keyStoreInputStream the InputStream to the KeyStore
*
* @deprecated Use {@link #setKeyStore(KeyStore)}
*/
@Deprecated
public void setKeyStoreInputStream(InputStream keyStoreInputStream)
{
checkNotStarted();
_keyStoreInputStream = keyStoreInputStream;
}
/* ------------------------------------------------------------ */
/**
* @return Alias of SSL certificate for the connector
@ -614,34 +566,6 @@ public class SslContextFactory extends AbstractLifeCycle
_trustStoreType = trustStoreType;
}
/* ------------------------------------------------------------ */
/** Get the _trustStoreInputStream.
* @return the _trustStoreInputStream
*
* @deprecated
*/
@Deprecated
public InputStream getTrustStoreInputStream()
{
checkKeyStore();
return _trustStoreInputStream;
}
/* ------------------------------------------------------------ */
/** Set the _trustStoreInputStream.
* @param trustStoreInputStream the InputStream to the TrustStore
*
* @deprecated
*/
@Deprecated
public void setTrustStoreInputStream(InputStream trustStoreInputStream)
{
checkNotStarted();
_trustStoreInputStream = trustStoreInputStream;
}
/* ------------------------------------------------------------ */
/**
* @return True if SSL needs client authentication.
@ -741,32 +665,6 @@ public class SslContextFactory extends AbstractLifeCycle
_validatePeerCerts = validatePeerCerts;
}
/* ------------------------------------------------------------ */
/**
* @return True if SSL re-negotiation is allowed (default false)
*/
public boolean isAllowRenegotiate()
{
return _allowRenegotiate;
}
/* ------------------------------------------------------------ */
/**
* Set if SSL re-negotiation is allowed. CVE-2009-3555 discovered
* a vulnerability in SSL/TLS with re-negotiation. If your JVM
* does not have CVE-2009-3555 fixed, then re-negotiation should
* not be allowed. CVE-2009-3555 was fixed in Sun java 1.6 with a ban
* of renegotiates in u19 and with RFC5746 in u22.
*
* @param allowRenegotiate
* true if re-negotiation is allowed (default false)
*/
public void setAllowRenegotiate(boolean allowRenegotiate)
{
checkNotStarted();
_allowRenegotiate = allowRenegotiate;
}
/* ------------------------------------------------------------ */
/**

View File

@ -53,20 +53,6 @@ public class SslContextFactoryTest
assertTrue(cf.getSslContext()!=null);
}
@Test
public void testNoTsStreamKs() throws Exception
{
InputStream keystoreInputStream = this.getClass().getResourceAsStream("keystore");
cf.setKeyStoreInputStream(keystoreInputStream);
cf.setKeyStorePassword("storepwd");
cf.setKeyManagerPassword("keypwd");
cf.start();
assertTrue(cf.getSslContext()!=null);
}
@Test
public void testNoTsSetKs() throws Exception
{