diff --git a/VERSION.txt b/VERSION.txt index 5fc112ca7e4..fbb32760ab0 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -10,6 +10,7 @@ jetty-7.0.0.M3-SNAPSHOT + Added ContinuationThrowable + 276545 Quoted cookie paths + 279725 Support 100 and 102 expectations + + Refactored AbstractBuffers to HttpBuffers for performance jetty-7.0.0.M2 18 May 2009 + JETTY-937 Work around Sun JVM bugs diff --git a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Connection.java b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Connection.java index 249c81301d0..3023e500944 100644 --- a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Connection.java +++ b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Connection.java @@ -22,9 +22,9 @@ import java.util.Collection; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpException; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.EndPoint; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; @@ -42,8 +42,8 @@ public class Ajp13Connection extends HttpConnection public Ajp13Connection(Connector connector, EndPoint endPoint, Server server) { super(connector, endPoint, server, - new Ajp13Parser(connector, endPoint), - new Ajp13Generator(connector, endPoint, connector.getHeaderBufferSize(), connector.getResponseBufferSize()), + new Ajp13Parser(connector.getRequestBuffers(), endPoint), + new Ajp13Generator(connector.getResponseBuffers(), endPoint), new Ajp13Request() ); diff --git a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Generator.java b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Generator.java index aca8bb100e7..96bb380c1be 100644 --- a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Generator.java +++ b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Generator.java @@ -108,9 +108,9 @@ public class Ajp13Generator extends AbstractGenerator private boolean _bufferPrepared = false; /* ------------------------------------------------------------ */ - public Ajp13Generator(Buffers buffers, EndPoint io, int headerBufferSize, int contentBufferSize) + public Ajp13Generator(Buffers buffers, EndPoint io) { - super(buffers, io, headerBufferSize, contentBufferSize); + super(buffers, io); } /* ------------------------------------------------------------ */ @@ -342,7 +342,7 @@ public class Ajp13Generator extends AbstractGenerator // get a header buffer if (_header == null) - _header = _buffers.getBuffer(_headerBufferSize); + _header = _buffers.getHeader(); Buffer tmpbuf = _buffer; _buffer = _header; @@ -658,7 +658,7 @@ public class Ajp13Generator extends AbstractGenerator if (_header == null) { - _header = _buffers.getBuffer(_headerBufferSize); + _header = _buffers.getHeader(); } if (_buffer == null && _header != null && _header.space() >= AJP13_MORE_CONTENT.length) @@ -709,7 +709,7 @@ public class Ajp13Generator extends AbstractGenerator { if (_buffer == null) { - _buffer = _buffers.getBuffer(_contentBufferSize); + _buffer = _buffers.getBuffer(); _buffer.setPutIndex(7); _buffer.setGetIndex(7); } @@ -782,14 +782,13 @@ public class Ajp13Generator extends AbstractGenerator public void sendCPong() throws IOException { - Buffer buff = _buffers.getBuffer(AJP13_CPONG_RESPONSE.length); + Buffer buff = _buffers.getBuffer(); buff.put(AJP13_CPONG_RESPONSE); // flushing cpong response do { _endp.flush(buff); - } while(buff.length() >0); _buffers.returnBuffer(buff); diff --git a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Parser.java b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Parser.java index 81ade2c722b..182ea032df5 100644 --- a/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Parser.java +++ b/jetty-ajp/src/main/java/org/eclipse/jetty/ajp/Ajp13Parser.java @@ -234,10 +234,8 @@ public class Ajp13Parser implements Parser if (_buffer == null) { if (_header == null) - { - _header = _buffers.getBuffer(Ajp13Packet.MAX_PACKET_SIZE); - _header.clear(); - } + _header = _buffers.getHeader(); + _buffer = _header; _tok0 = new View(_header); _tok1 = new View(_header); @@ -491,7 +489,7 @@ public class Ajp13Parser implements Parser _generator.getBodyChunk(); if (_buffers != null && _body == null && _buffer == _header && _header.length() <= 0) { - _body = _buffers.getBuffer(Ajp13Packet.MAX_PACKET_SIZE); + _body = _buffers.getBuffer(); _body.clear(); } _state = STATE_AJP13CHUNK_START; @@ -503,7 +501,7 @@ public class Ajp13Parser implements Parser if (_buffers != null && _body == null && _buffer == _header && _contentLength > (_header.capacity() - _header.getIndex())) { - _body = _buffers.getBuffer(Ajp13Packet.MAX_PACKET_SIZE); + _body = _buffers.getBuffer(); _body.clear(); } diff --git a/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/TestAjpParser.java b/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/TestAjpParser.java index facba13082d..710b4264cc2 100644 --- a/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/TestAjpParser.java +++ b/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/TestAjpParser.java @@ -33,13 +33,13 @@ public class TestAjpParser extends TestCase byte[] src = TypeUtil.fromHexString(packet); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); EndPoint endp = new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseAvailable(); @@ -51,12 +51,11 @@ public class TestAjpParser extends TestCase String packet="1234020102020008485454502f312e3100000f2f6363632d7777777777772f61616100000c38382e3838382e38382e383830ffff00116363632e6363636363636363632e636f6d0001bb010009a00b00116363632e6363636363636363632e636f6d00a00e005a4d6f7a696c6c612f352e30202857696e646f77733b20553b2057696e646f7773204e5420352e313b20656e2d55533b2072763a312e382e312e3129204765636b6f2f32303036313230342046697265666f782f322e302e302e3100a0010063746578742f786d6c2c6170706c69636174696f6e2f786d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c746578742f68746d6c3b713d302e392c746578742f706c61696e3b713d302e382c696d6167652f706e672c2a2f2a3b713d302e3500a004000e656e2d75732c656e3b713d302e3500a003000c677a69702c6465666c61746500a002001e49534f2d383835392d312c7574662d383b713d302e372c2a3b713d302e3700000a4b6565702d416c69766500000333303000a006000a6b6565702d616c69766500000c4d61782d466f7277617264730000023130000800124448452d5253412d4145533235362d5348410009004039324643303544413043444141443232303137413743443141453939353132413330443938363838423843433041454643364231363035323543433232353341000b0100ff"; byte[] src=TypeUtil.fromHexString(packet); ByteArrayBuffer buffer=new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[] - { buffer }); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); EndPoint endp=new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parse(); assertTrue(true); } @@ -66,12 +65,11 @@ public class TestAjpParser extends TestCase String packet="1234028f02020008485454502f312e3100000d2f666f726d746573742e6a737000000d3139322e3136382e342e31383000ffff00107777772e777265636b6167652e6f726700005000000aa0010063746578742f786d6c2c6170706c69636174696f6e2f786d6c2c6170706c69636174696f6e2f7868746d6c2b786d6c2c746578742f68746d6c3b713d302e392c746578742f706c61696e3b713d302e382c696d6167652f706e672c2a2f2a3b713d302e3500a00200075554462d382c2a00a003000c677a69702c6465666c61746500a004000e656e2d67622c656e3b713d302e3500a006000a6b6565702d616c69766500a00900f95048505345535349443d37626361383232616638333466316465373663633630336366636435313938633b20667041757468436f6f6b69653d433035383430394537393344364245434633324230353234344242303039343230383344443645443533304230454637464137414544413745453231313538333745363033454435364332364446353531383635333335423433374531423637414641343533364345304546323342333642323133374243423932333943363631433131443330393842333938414546334546334146454344423746353842443b204a53455353494f4e49443d7365366331623864663432762e6a657474793300a00b00107777772e777265636b6167652e6f726700000a6b6565702d616c69766500000333303000a00e00654d6f7a696c6c612f352e3020285831313b20553b204c696e7578207838365f36343b20656e2d55533b2072763a312e382e302e3929204765636b6f2f3230303631323035202844656269616e2d312e382e302e392d3129204570697068616e792f322e313400a008000130000600066a657474793300ff"; byte[] src=TypeUtil.fromHexString(packet); ByteArrayBuffer buffer=new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[] - { buffer }); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); EndPoint endp=new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parse(); assertTrue(true); } @@ -83,13 +81,13 @@ public class TestAjpParser extends TestCase byte[] src = TypeUtil.fromHexString(packet); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); EndPoint endp = new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseAvailable(); @@ -102,13 +100,13 @@ public class TestAjpParser extends TestCase byte[] src = TypeUtil.fromHexString(packet); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); EndPoint endp = new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseAvailable(); @@ -129,14 +127,14 @@ public class TestAjpParser extends TestCase System.arraycopy(src,src.length-f,frag1,0,f); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); ByteArrayEndPoint endp = new ByteArrayEndPoint(frag0,Ajp13Packet.MAX_PACKET_SIZE); endp.setNonBlocking(true); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseNext(); endp.setIn(new ByteArrayBuffer(frag1)); @@ -153,13 +151,12 @@ public class TestAjpParser extends TestCase String packet=getTestHeader(); byte[] src=TypeUtil.fromHexString(packet); ByteArrayBuffer buffer=new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[] - { buffer }); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); ByteArrayEndPoint endp=new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); endp.setNonBlocking(true); final int count[]={0}; - Ajp13Generator gen = new Ajp13Generator(buffers,endp,0,0) + Ajp13Generator gen = new Ajp13Generator(buffers,endp) { public void getBodyChunk() throws IOException { @@ -198,13 +195,12 @@ public class TestAjpParser extends TestCase String packet="123400ff02040008485454502f312e3100000f2f746573742f64756d702f696e666f0000093132372e302e302e3100ffff00096c6f63616c686f7374000050000007a00e000d4a6176612f312e352e305f313100a00b00096c6f63616c686f737400a0010034746578742f68746d6c2c20696d6167652f6769662c20696d6167652f6a7065672c202a3b20713d2e322c202a2f2a3b20713d2e3200a006000a6b6565702d616c69766500a00700216170706c69636174696f6e2f782d7777772d666f726d2d75726c656e636f6465640000115472616e736665722d456e636f64696e670000076368756e6b656400000c4d61782d466f727761726473000002313000ff"; byte[] src=TypeUtil.fromHexString(packet); ByteArrayBuffer buffer=new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[] - { buffer }); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); ByteArrayEndPoint endp=new ByteArrayEndPoint(src,Ajp13Packet.MAX_PACKET_SIZE); endp.setNonBlocking(true); final int count[]={0}; - Ajp13Generator gen = new Ajp13Generator(buffers,endp,0,0) + Ajp13Generator gen = new Ajp13Generator(buffers,endp) { public void getBodyChunk() throws IOException { @@ -258,14 +254,14 @@ public class TestAjpParser extends TestCase System.arraycopy(src,src.length-f,frag1,0,f); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); ByteArrayEndPoint endp = new ByteArrayEndPoint(frag0,Ajp13Packet.MAX_PACKET_SIZE); endp.setNonBlocking(true); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseNext(); endp.setIn(new ByteArrayBuffer(frag1)); @@ -291,14 +287,14 @@ public class TestAjpParser extends TestCase System.arraycopy(src,src.length-f,frag1,0,f); ByteArrayBuffer buffer= new ByteArrayBuffer(Ajp13Packet.MAX_PACKET_SIZE); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); ByteArrayEndPoint endp = new ByteArrayEndPoint(frag0,Ajp13Packet.MAX_PACKET_SIZE); endp.setNonBlocking(true); Ajp13Parser parser = new Ajp13Parser(buffers,endp); parser.setEventHandler(new EH()); - parser.setGenerator(new Ajp13Generator(buffers,endp,0,0)); + parser.setGenerator(new Ajp13Generator(buffers,endp)); parser.parseNext(); endp.setIn(new ByteArrayBuffer(frag1)); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java index e07515a62b3..07c571f88ea 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpClient.java @@ -35,8 +35,8 @@ import javax.net.ssl.X509TrustManager; import org.eclipse.jetty.client.security.Authorization; import org.eclipse.jetty.client.security.RealmResolver; +import org.eclipse.jetty.http.HttpBuffers; import org.eclipse.jetty.http.HttpSchemes; -import org.eclipse.jetty.io.AbstractBuffers; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.io.nio.DirectNIOBuffer; @@ -78,7 +78,7 @@ import org.eclipse.jetty.util.thread.Timeout; * @see {@link HttpExchange} * @see {@link HttpDestination} */ -public class HttpClient extends AbstractBuffers implements Attributes +public class HttpClient extends HttpBuffers implements Attributes { public static final int CONNECTOR_SOCKET = 0; public static final int CONNECTOR_SELECT_CHANNEL = 2; @@ -341,26 +341,50 @@ public class HttpClient extends AbstractBuffers implements Attributes /* ------------------------------------------------------------ */ /** - * Create a new NIO buffer. If using direct buffers, it will create a direct - * NIO buffer, other than an indirect buffer. + * @see org.eclipse.jetty.http.HttpBuffers#newRequestBuffer(int) */ - public Buffer newBuffer(int size) + @Override + protected Buffer newRequestBuffer(int size) { - if (_connectorType != CONNECTOR_SOCKET) - { - Buffer buf = null; - if (size==getHeaderBufferSize()) - buf=new IndirectNIOBuffer(size); - else if (_useDirectBuffers) - buf=new DirectNIOBuffer(size); - else - buf=new IndirectNIOBuffer(size); - return buf; - } - else - { + if (_connectorType == CONNECTOR_SOCKET) return new ByteArrayBuffer(size); - } + return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.http.HttpBuffers#newRequestHeader(int) + */ + @Override + protected Buffer newRequestHeader(int size) + { + if (_connectorType == CONNECTOR_SOCKET) + return new ByteArrayBuffer(size); + return new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.http.HttpBuffers#newResponseBuffer(int) + */ + @Override + protected Buffer newResponseBuffer(int size) + { + if (_connectorType == CONNECTOR_SOCKET) + return new ByteArrayBuffer(size); + return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.http.HttpBuffers#newResponseHeader(int) + */ + @Override + protected Buffer newResponseHeader(int size) + { + if (_connectorType == CONNECTOR_SOCKET) + return new ByteArrayBuffer(size); + return new IndirectNIOBuffer(size); } /* ------------------------------------------------------------ */ diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java index 3db6db700a9..5555ff2e501 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpConnection.java @@ -108,11 +108,11 @@ public class HttpConnection implements Connection }; /* ------------------------------------------------------------ */ - HttpConnection(Buffers buffers, EndPoint endp, int hbs, int cbs) + HttpConnection(Buffers requestBuffers, Buffers responseBuffers, EndPoint endp) { _endp = endp; - _generator = new HttpGenerator(buffers,endp,hbs,cbs); - _parser = new HttpParser(buffers,endp,new Handler(),hbs,cbs); + _generator = new HttpGenerator(requestBuffers,endp); + _parser = new HttpParser(responseBuffers,endp,new Handler()); } public void setReserved (boolean reserved) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/SelectConnector.java b/jetty-client/src/main/java/org/eclipse/jetty/client/SelectConnector.java index cf079389a89..6c050f5c896 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/SelectConnector.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/SelectConnector.java @@ -19,14 +19,17 @@ import java.nio.channels.SocketChannel; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLSession; +import org.eclipse.jetty.http.HttpBuffers; import org.eclipse.jetty.http.HttpMethods; import org.eclipse.jetty.http.HttpVersions; import org.eclipse.jetty.http.ssl.SslSelectChannelEndPoint; -import org.eclipse.jetty.io.AbstractBuffers; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Buffers; import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.ThreadLocalBuffers; +import org.eclipse.jetty.io.nio.DirectNIOBuffer; import org.eclipse.jetty.io.nio.IndirectNIOBuffer; import org.eclipse.jetty.io.nio.SelectChannelEndPoint; import org.eclipse.jetty.io.nio.SelectorManager; @@ -40,7 +43,7 @@ class SelectConnector extends AbstractLifeCycle implements HttpClient.Connector, private Buffers _sslBuffers; private boolean _blockingConnect; - SelectorManager _selectorManager=new Manager(); + Manager _selectorManager=new Manager(); /** * @param httpClient @@ -71,7 +74,32 @@ class SelectConnector extends AbstractLifeCycle implements HttpClient.Connector, /* ------------------------------------------------------------ */ protected void doStart() throws Exception { + super.doStart(); + _selectorManager.start(); + + SSLEngine sslEngine=_selectorManager.newSslEngine(); + SSLSession ssl_session=sslEngine.getSession(); + + ThreadLocalBuffers buffers = new ThreadLocalBuffers() + { + @Override + protected Buffer newBuffer(int size) + { + // TODO indirect? + return new DirectNIOBuffer(size); + } + @Override + protected Buffer newHeader(int size) + { + // TODO indirect? + return new DirectNIOBuffer(size); + } + }; + buffers.setBufferSize(ssl_session.getApplicationBufferSize()); + buffers.setHeaderSize(ssl_session.getPacketBufferSize()); + _sslBuffers=buffers; + _httpClient._threadPool.dispatch(this); } @@ -134,7 +162,7 @@ class SelectConnector extends AbstractLifeCycle implements HttpClient.Connector, protected Connection newConnection(SocketChannel channel, SelectChannelEndPoint endpoint) { - return new HttpConnection(_httpClient,endpoint,SelectConnector.this._httpClient.getHeaderBufferSize(),SelectConnector.this._httpClient.getRequestBufferSize()); + return new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint); } protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException @@ -180,29 +208,8 @@ class SelectConnector extends AbstractLifeCycle implements HttpClient.Connector, sslEngine.setUseClientMode(true); sslEngine.beginHandshake(); - if (_sslBuffers==null) - { - AbstractBuffers buffers = new AbstractBuffers() - { - public Buffer newBuffer( int size ) - { - return new IndirectNIOBuffer( size); - } - }; - buffers.setRequestBufferSize( sslEngine.getSession().getPacketBufferSize()); - buffers.setResponseBufferSize(sslEngine.getSession().getApplicationBufferSize()); - - try - { - buffers.start(); - } - catch(Exception e) - { - throw new IllegalStateException(e); - } - _sslBuffers=buffers; - } + return sslEngine; } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java b/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java index 275a7b26513..ace94598510 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/SocketConnector.java @@ -59,7 +59,7 @@ class SocketConnector extends AbstractLifeCycle implements HttpClient.Connector EndPoint endpoint=new SocketEndPoint(socket); - final HttpConnection connection=new HttpConnection(_httpClient,endpoint,_httpClient.getHeaderBufferSize(),_httpClient.getRequestBufferSize()); + final HttpConnection connection=new HttpConnection(_httpClient.getRequestBuffers(),_httpClient.getResponseBuffers(),endpoint); connection.setDestination(destination); destination.onNewConnection(connection); _httpClient.getThreadPool().dispatch(new Runnable() diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/AbstractGenerator.java b/jetty-http/src/main/java/org/eclipse/jetty/http/AbstractGenerator.java index 9ebb9d5ee5b..57217e23f13 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/AbstractGenerator.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/AbstractGenerator.java @@ -49,8 +49,6 @@ public abstract class AbstractGenerator implements Generator protected final Buffers _buffers; // source of buffers protected final EndPoint _endp; - protected final int _headerBufferSize; - protected int _contentBufferSize; protected int _state = STATE_HEADER; @@ -83,12 +81,10 @@ public abstract class AbstractGenerator implements Generator * @param headerBufferSize Size of the buffer to allocate for HTTP header * @param contentBufferSize Size of the buffer to allocate for HTTP content */ - public AbstractGenerator(Buffers buffers, EndPoint io, int headerBufferSize, int contentBufferSize) + public AbstractGenerator(Buffers buffers, EndPoint io) { this._buffers = buffers; this._endp = io; - _headerBufferSize=headerBufferSize; - _contentBufferSize=contentBufferSize; } /* ------------------------------------------------------------------------------- */ @@ -112,17 +108,15 @@ public abstract class AbstractGenerator implements Generator _contentLength = HttpTokens.UNKNOWN_CONTENT; // always return the buffer - Buffer buffer=_buffer; + if (_buffer!=null) + _buffers.returnBuffer(_buffer); _buffer=null; - if (buffer!=null && _header!=buffer) - _buffers.returnBuffer(buffer); - + if (returnBuffers) { - buffer=_header; + if (_header!=null) + _buffers.returnBuffer(_header); _header=null; - if (buffer!=null) - _buffers.returnBuffer(buffer); } else if (_header != null) _header.clear(); @@ -152,7 +146,9 @@ public abstract class AbstractGenerator implements Generator */ public int getContentBufferSize() { - return _contentBufferSize; + if (_buffer==null) + _buffer=_buffers.getBuffer(); + return _buffer.capacity(); } /* ------------------------------------------------------------ */ @@ -161,12 +157,13 @@ public abstract class AbstractGenerator implements Generator */ public void increaseContentBufferSize(int contentBufferSize) { - if (contentBufferSize > _contentBufferSize) + if (_buffer==null) + _buffer=_buffers.getBuffer(); + if (contentBufferSize > _buffer.capacity()) { - _contentBufferSize = contentBufferSize; if (_buffer != null) { - Buffer nb = _buffers.getBuffer(_contentBufferSize); + Buffer nb = _buffers.getBuffer(contentBufferSize); nb.put(_buffer); _buffers.returnBuffer(_buffer); _buffer = nb; @@ -312,8 +309,10 @@ public abstract class AbstractGenerator implements Generator if (reason!=null) { int len=reason.length(); - if (len>_headerBufferSize/2) - len=_headerBufferSize/2; + + // TODO don't hard code + if (len>1024) + len=1024; _reason=new ByteArrayBuffer(len); for (int i=0;i=(_header.capacity()-_header.getIndex()))) - _body=_buffers.getBuffer(_contentBufferSize); + _body=_buffers.getBuffer(); _handler.headerComplete(); // May recurse here ! break; } @@ -957,7 +951,6 @@ public class HttpParser implements Parser } } - if (_header!=null) { _header.setMarkIndex(-1); @@ -965,7 +958,6 @@ public class HttpParser implements Parser { _buffers.returnBuffer(_header); _header=null; - _buffer=null; } else { @@ -1004,7 +996,7 @@ public class HttpParser implements Parser { if (_header == null) { - _header=_buffers.getBuffer(_headerBufferSize); + _header=_buffers.getHeader(); } return _header; } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/ssl/SslSelectChannelEndPoint.java b/jetty-http/src/main/java/org/eclipse/jetty/http/ssl/SslSelectChannelEndPoint.java index 4c548892fea..e0d28aab4d6 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/ssl/SslSelectChannelEndPoint.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/ssl/SslSelectChannelEndPoint.java @@ -26,6 +26,7 @@ import javax.net.ssl.SSLEngineResult.HandshakeStatus; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Buffers; +import org.eclipse.jetty.io.nio.DirectNIOBuffer; import org.eclipse.jetty.io.nio.NIOBuffer; import org.eclipse.jetty.io.nio.SelectChannelEndPoint; import org.eclipse.jetty.io.nio.SelectorManager; @@ -86,9 +87,9 @@ public class SslSelectChannelEndPoint extends SelectChannelEndPoint _session=engine.getSession(); // TODO pool buffers and use only when needed. - _outNIOBuffer=(NIOBuffer)buffers.getBuffer(_session.getPacketBufferSize()); + _outNIOBuffer=(NIOBuffer)_buffers.getBuffer(_session.getPacketBufferSize()); _outBuffer=_outNIOBuffer.getByteBuffer(); - _inNIOBuffer=(NIOBuffer)buffers.getBuffer(_session.getPacketBufferSize()); + _inNIOBuffer=(NIOBuffer)_buffers.getBuffer(_session.getPacketBufferSize()); _inBuffer=_inNIOBuffer.getByteBuffer(); // h.append("CONSTRUCTED\n"); diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorClientTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorClientTest.java index 8ee81d80482..e95eab6d8fc 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorClientTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorClientTest.java @@ -39,7 +39,7 @@ public class HttpGeneratorClientTest extends TestCase Buffer bb=new ByteArrayBuffer(8096); Buffer sb=new ByteArrayBuffer(1500); ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096); - HttpGenerator generator = new HttpGenerator(new SimpleBuffers(new Buffer[]{sb,bb}),endp, sb.capacity(), bb.capacity()); + HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp); generator.setRequest("GET","/usr"); @@ -67,7 +67,7 @@ public class HttpGeneratorClientTest extends TestCase Buffer bb=new ByteArrayBuffer(8096); Buffer sb=new ByteArrayBuffer(1500); ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096); - HttpGenerator generator = new HttpGenerator(new SimpleBuffers(new Buffer[]{sb,bb}),endp, sb.capacity(), bb.capacity()); + HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp); generator.setRequest("GET","/usr"); @@ -94,7 +94,7 @@ public class HttpGeneratorClientTest extends TestCase Buffer bb=new ByteArrayBuffer(8096); Buffer sb=new ByteArrayBuffer(1500); ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096); - HttpGenerator generator = new HttpGenerator(new SimpleBuffers(new Buffer[]{sb,bb}),endp, sb.capacity(), bb.capacity()); + HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp); generator.setRequest("GET","/usr"); @@ -122,7 +122,7 @@ public class HttpGeneratorClientTest extends TestCase Buffer sb=new ByteArrayBuffer(1500); HttpFields fields = new HttpFields(); ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096); - HttpGenerator hb = new HttpGenerator(new SimpleBuffers(new Buffer[]{sb,bb}),endp, sb.capacity(), bb.capacity()); + HttpGenerator hb = new HttpGenerator(new SimpleBuffers(sb,bb),endp); Handler handler = new Handler(); HttpParser parser=null; diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorTest.java index a8b19ddf1b9..c57a2fa9012 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpGeneratorTest.java @@ -47,7 +47,7 @@ public class HttpGeneratorTest extends TestCase Buffer sb=new ByteArrayBuffer(1500); HttpFields fields = new HttpFields(); ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],4096); - HttpGenerator hb = new HttpGenerator(new SimpleBuffers(new Buffer[]{sb,bb}),endp, sb.capacity(), bb.capacity()); + HttpGenerator hb = new HttpGenerator(new SimpleBuffers(sb,bb),endp); Handler handler = new Handler(); HttpParser parser=null; diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java index 28a6c1a5ab7..6069ea59c70 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java @@ -69,10 +69,10 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("POST /foo HTTP/1.0\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("POST", f0); assertEquals("/foo", f1); @@ -86,11 +86,11 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("GET /999\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); f2= null; Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("GET", f0); assertEquals("/999", f1); @@ -104,11 +104,11 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("POST /222 \015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); f2= null; Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("POST", f0); assertEquals("/222", f1); @@ -122,10 +122,10 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("POST", f0); assertEquals("/fo\u0690", f1); @@ -139,10 +139,10 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("POST", f0); assertEquals("/foo?param=\u0690", f1); @@ -156,10 +156,10 @@ public class HttpParserTest extends TestCase StringEndPoint io=new StringEndPoint(); io.setInput("CONNECT 192.168.1.2:80 HTTP/1.1\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertTrue(handler.request); assertEquals("CONNECT", f0); @@ -184,10 +184,10 @@ public class HttpParserTest extends TestCase + "Server5: notServer\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("GET", f0); assertEquals("/", f1); @@ -223,10 +223,10 @@ public class HttpParserTest extends TestCase + "0\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); ByteArrayBuffer content=new ByteArrayBuffer(8192); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer,content}); + SimpleBuffers buffers=new SimpleBuffers(buffer,content); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), content.capacity()); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("GET", f0); assertEquals("/chunk", f1); @@ -263,10 +263,10 @@ public class HttpParserTest extends TestCase ByteArrayBuffer buffer= new ByteArrayBuffer(4096); ByteArrayBuffer content=new ByteArrayBuffer(8192); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer,content}); + SimpleBuffers buffers=new SimpleBuffers(buffer,content); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), content.capacity()); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("GET", f0); assertEquals("/mp", f1); @@ -341,10 +341,10 @@ public class HttpParserTest extends TestCase { ByteArrayBuffer buffer= new ByteArrayBuffer(tests[t]); ByteArrayBuffer content=new ByteArrayBuffer(8192); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer,content}); + SimpleBuffers buffers=new SimpleBuffers(buffer,content); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), content.capacity()); + HttpParser parser= new HttpParser(buffers,io, handler); io.setInput(http); @@ -396,10 +396,10 @@ public class HttpParserTest extends TestCase + "\015\012" + "0123456789\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("HTTP/1.1", f0); assertEquals("200", f1); @@ -418,10 +418,10 @@ public class HttpParserTest extends TestCase + "Connection: close\015\012" + "\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("HTTP/1.1", f0); assertEquals("304", f1); @@ -444,10 +444,10 @@ public class HttpParserTest extends TestCase + "\015\012" + "0123456789\015\012"); ByteArrayBuffer buffer= new ByteArrayBuffer(4096); - SimpleBuffers buffers=new SimpleBuffers(new Buffer[]{buffer}); + SimpleBuffers buffers=new SimpleBuffers(buffer,null); Handler handler = new Handler(); - HttpParser parser= new HttpParser(buffers,io, handler, buffer.capacity(), 0); + HttpParser parser= new HttpParser(buffers,io, handler); parser.parse(); assertEquals("HTTP/1.1", f0); assertEquals("204", f1); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java b/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java deleted file mode 100644 index bf4cf05baf2..00000000000 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/AbstractBuffers.java +++ /dev/null @@ -1,191 +0,0 @@ -// ======================================================================== -// Copyright (c) 2004-2009 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.io; - -import org.eclipse.jetty.util.component.AbstractLifeCycle; - -/* ------------------------------------------------------------ */ -/** Abstract Buffer pool. - * simple unbounded pool of buffers for header, request and response sizes. - * - */ -public abstract class AbstractBuffers extends AbstractLifeCycle implements Buffers -{ - private int _headerBufferSize=6*1024; - private int _requestBufferSize=8*1024; - private int _responseBufferSize=12*1024; - - final static private int __HEADER=0; - final static private int __REQUEST=1; - final static private int __RESPONSE=2; - final static private int __OTHER=3; - final private int[] _pool={2,1,1,1}; - - private final ThreadLocal _buffers=new ThreadLocal() - { - protected Object initialValue() - { - return new ThreadBuffers(_pool[__HEADER],_pool[__REQUEST],_pool[__RESPONSE],_pool[__OTHER]); - } - }; - - public AbstractBuffers() - { - super(); - } - - public Buffer getBuffer(final int size ) - { - final int set = (size==_headerBufferSize)?__HEADER - :(size==_responseBufferSize)?__RESPONSE - :(size==_requestBufferSize)?__REQUEST:__OTHER; - - final ThreadBuffers thread_buffers = (ThreadBuffers)_buffers.get(); - - final Buffer[] buffers=thread_buffers._buffers[set]; - for (int i=0;i _buffers=new ThreadLocal() + { + protected ThreadBuffers initialValue() + { + return new ThreadBuffers(); + } + }; + + public Buffer getBuffer() + { + ThreadBuffers buffers = (ThreadBuffers)_buffers.get(); + if (buffers._buffer!=null) + { + Buffer b=buffers._buffer; + buffers._buffer=null; + return b; + } + + if (buffers._other!=null && buffers._other.capacity()==_bufferSize) + { + Buffer b=buffers._other; + buffers._other=null; + return b; + } + + return newBuffer(_bufferSize); + } + + public Buffer getHeader() + { + ThreadBuffers buffers = (ThreadBuffers)_buffers.get(); + if (buffers._header!=null) + { + Buffer b=buffers._header; + buffers._header=null; + return b; + } + + if (buffers._other!=null && buffers._other.capacity()==_headerSize) + { + Buffer b=buffers._other; + buffers._other=null; + return b; + } + + return newHeader(_headerSize); + } + + public Buffer getBuffer(int size) + { + ThreadBuffers buffers = (ThreadBuffers)_buffers.get(); + if (buffers._other!=null && buffers._other.capacity()==size) + { + Buffer b=buffers._other; + buffers._other=null; + return b; + } + + return newBuffer(size); + } + + public void returnBuffer(Buffer buffer) + { + buffer.clear(); + if (buffer.isVolatile() || buffer.isImmutable()) + return; + + int size=buffer.capacity(); + + ThreadBuffers buffers = (ThreadBuffers)_buffers.get(); + + if (size==_bufferSize && buffers._buffer==null) + { + buffers._buffer=buffer; + } + else if (size==_headerSize && buffers._header==null) + { + buffers._header=buffer; + } + else + { + buffers._other=buffer; + } + } + + + + /** + * @return Returns the buffer size in bytes. + */ + public int getBufferSize() + { + return _bufferSize; + } + + /** + * @return Returns the header size in bytes. + */ + public int getHeaderSize() + { + return _headerSize; + } + + protected abstract Buffer newBuffer(int size); + + protected abstract Buffer newHeader(int size); + + /** + * @param size The buffer size in bytes + */ + public void setBufferSize( int size ) + { + _bufferSize = size; + } + + /** + * @param size The header size in bytes + */ + public void setHeaderSize( int size ) + { + _headerSize = size; + } + + protected static class ThreadBuffers + { + Buffer _buffer; + Buffer _header; + Buffer _other; + } +} diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java b/jetty-io/src/main/java/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java index e07e2fab34e..43d154034a7 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @@ -23,7 +23,6 @@ import org.eclipse.jetty.io.AsyncEndPoint; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EofException; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.io.nio.SelectorManager.SelectSet; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.thread.Timeout; @@ -432,9 +431,10 @@ public class SelectChannelEndPoint extends ChannelEndPoint implements Runnable, try{close();} catch(IOException e2){Log.ignore(e2);} } - catch (HttpException e) + catch (IOException e) { - Log.debug("BAD", e); + Log.warn(e.toString()); + Log.debug(e); try{close();} catch(IOException e2){Log.ignore(e2);} } diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/AbstractBuffersTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/ThreadLocalBuffersTest.java similarity index 89% rename from jetty-io/src/test/java/org/eclipse/jetty/io/AbstractBuffersTest.java rename to jetty-io/src/test/java/org/eclipse/jetty/io/ThreadLocalBuffersTest.java index 4bbf1d500e9..204070ddce0 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/AbstractBuffersTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/ThreadLocalBuffersTest.java @@ -20,13 +20,13 @@ import java.util.concurrent.atomic.AtomicLong; import junit.framework.TestCase; -public class AbstractBuffersTest +public class ThreadLocalBuffersTest extends TestCase { public boolean _stress = Boolean.getBoolean("STRESS"); private int _headerBufferSize = 6 * 1024; - InnerAbstractBuffers buffers; + InnerBuffers httpBuffers; List threadList = new ArrayList(); @@ -63,7 +63,7 @@ public class AbstractBuffersTest { threadList.clear(); buffersRetrieved = new AtomicLong( 0 ); - buffers = new InnerAbstractBuffers(); + httpBuffers = new InnerBuffers(); for ( int i = 0; i < numThreads; ++i ) { @@ -115,11 +115,16 @@ public class AbstractBuffersTest } - class InnerAbstractBuffers - extends AbstractBuffers + class InnerBuffers extends ThreadLocalBuffers { + @Override + protected Buffer newBuffer(int size) + { + return new ByteArrayBuffer( size ); + } - public Buffer newBuffer( int size ) + @Override + protected Buffer newHeader(int size) { return new ByteArrayBuffer( size ); } @@ -153,7 +158,7 @@ public class AbstractBuffersTest if ( runTest ) { - Buffer buf = buffers.getBuffer( _headerBufferSize ); + Buffer buf = httpBuffers.getHeader(); buffersRetrieved.getAndIncrement(); @@ -162,7 +167,7 @@ public class AbstractBuffersTest // sleep( threadWaitTime ); - buffers.returnBuffer( buf ); + httpBuffers.returnBuffer(buf); } else { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java index 041d1cd1c3f..dcf3dd78311 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractConnector.java @@ -20,10 +20,12 @@ import java.net.UnknownHostException; import javax.servlet.ServletRequest; +import org.eclipse.jetty.http.HttpBuffers; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeaders; import org.eclipse.jetty.http.HttpSchemes; -import org.eclipse.jetty.io.AbstractBuffers; +import org.eclipse.jetty.io.Buffer; +import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.util.component.LifeCycle; @@ -43,7 +45,7 @@ import org.eclipse.jetty.util.thread.ThreadPool; * * */ -public abstract class AbstractConnector extends AbstractBuffers implements Connector +public abstract class AbstractConnector extends HttpBuffers implements Connector { private String _name; @@ -98,6 +100,37 @@ public abstract class AbstractConnector extends AbstractBuffers implements Conne { } + /* ------------------------------------------------------------------------------- */ + public final Buffer newBuffer(int size) + { + // TODO remove once no overrides established + return null; + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newRequestBuffer(int size) + { + return new ByteArrayBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newRequestHeader(int size) + { + return new ByteArrayBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newResponseBuffer(int size) + { + return new ByteArrayBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newResponseHeader(int size) + { + return new ByteArrayBuffer(size); + } + /* ------------------------------------------------------------------------------- */ /* */ diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java index b06e15d283b..66085ee63d3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.util.thread.ThreadPool; * * */ -public interface Connector extends LifeCycle, Buffers +public interface Connector extends LifeCycle { /* ------------------------------------------------------------ */ /** @@ -59,17 +59,42 @@ public interface Connector extends LifeCycle, Buffers /* ------------------------------------------------------------ */ /** - * @return Returns the headerBufferSize. + * @return Returns the request header buffer size in bytes. */ - int getHeaderBufferSize(); + int getRequestHeaderSize(); /* ------------------------------------------------------------ */ /** - * Set the size of the buffer to be used for request and response headers. - * An idle connection will at most have one buffer of this size allocated. - * @param headerBufferSize The headerBufferSize to set. + * Set the size of the buffer to be used for request headers. + * @param size The size in bytes. */ - void setHeaderBufferSize(int headerBufferSize); + void setRequestHeaderSize(int size); + + /* ------------------------------------------------------------ */ + /** + * @return Returns the response header buffer size in bytes. + */ + int getResponseHeaderSize(); + + /* ------------------------------------------------------------ */ + /** + * Set the size of the buffer to be used for request headers. + * @param size The size in bytes. + */ + void setResponseHeaderSize(int size); + + + /* ------------------------------------------------------------ */ + /** + * @return factory for request buffers + */ + Buffers getRequestBuffers(); + + /* ------------------------------------------------------------ */ + /** + * @return factory for response buffers + */ + Buffers getResponseBuffers(); /* ------------------------------------------------------------ */ diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConnection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConnection.java index 53c4003144f..c728875268e 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConnection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConnection.java @@ -25,7 +25,9 @@ import org.eclipse.jetty.continuation.ContinuationThrowable; import org.eclipse.jetty.http.AbstractGenerator; import org.eclipse.jetty.http.EncodedHttpURI; import org.eclipse.jetty.http.Generator; +import org.eclipse.jetty.http.HttpBuffers; import org.eclipse.jetty.http.HttpContent; +import org.eclipse.jetty.http.HttpException; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpGenerator; import org.eclipse.jetty.http.HttpHeaderValues; @@ -41,7 +43,6 @@ import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.io.BufferCache.CachedBuffer; import org.eclipse.jetty.io.nio.SelectChannelEndPoint; import org.eclipse.jetty.util.QuotedStringTokenizer; @@ -128,12 +129,13 @@ public class HttpConnection implements Connection _uri = URIUtil.__CHARSET==StringUtil.__UTF8?new HttpURI():new EncodedHttpURI(URIUtil.__CHARSET); _connector = connector; _endp = endpoint; - _parser = new HttpParser(_connector, endpoint, new RequestHandler(), _connector.getHeaderBufferSize(), _connector.getRequestBufferSize()); + HttpBuffers ab = (HttpBuffers)_connector; + _parser = new HttpParser(ab.getRequestBuffers(), endpoint, new RequestHandler()); _requestFields = new HttpFields(); _responseFields = new HttpFields(); _request = new Request(this); _response = new Response(this); - _generator = new HttpGenerator(_connector, _endp, _connector.getHeaderBufferSize(), _connector.getResponseBufferSize()); + _generator = new HttpGenerator(ab.getResponseBuffers(), _endp); _generator.setSendServerVersion(server.getSendServerVersion()); _server = server; } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java index cb845e26835..7fd1976b9d3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java @@ -149,12 +149,6 @@ public class LocalConnector extends AbstractConnector return _out; } - /* ------------------------------------------------------------ */ - public Buffer newBuffer(int size) - { - return new ByteArrayBuffer(size); - } - /* ------------------------------------------------------------ */ protected void accept(int acceptorID) throws IOException, InterruptedException { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/bio/SocketConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/bio/SocketConnector.java index 543d8ea1b5f..e0370279419 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/bio/SocketConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/bio/SocketConnector.java @@ -21,11 +21,11 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import org.eclipse.jetty.http.HttpException; import org.eclipse.jetty.io.Buffer; import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.io.bio.SocketEndPoint; import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.HttpConnection; @@ -111,12 +111,6 @@ public class SocketConnector extends AbstractConnector return new HttpConnection(this, endpoint, getServer()); } - /* ------------------------------------------------------------------------------- */ - public Buffer newBuffer(int size) - { - return new ByteArrayBuffer(size); - } - /* ------------------------------------------------------------------------------- */ public void customize(EndPoint endpoint, Request request) throws IOException diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index ce17f33cf68..9f9d61715a8 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -45,9 +45,9 @@ import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpException; import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.io.Buffer; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.server.Dispatcher; import org.eclipse.jetty.server.DispatcherType; import org.eclipse.jetty.server.Handler; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/nio/AbstractNIOConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/nio/AbstractNIOConnector.java index 6876144b714..40611a22ccb 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/nio/AbstractNIOConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/nio/AbstractNIOConnector.java @@ -46,32 +46,44 @@ public abstract class AbstractNIOConnector extends AbstractConnector implements { _useDirectBuffers=direct; } - - /* ------------------------------------------------------------------------------- */ - public Buffer newBuffer(int size) - { - // TODO - // Header buffers always byte array buffers (efficiency of random access) - // There are lots of things to consider here... DIRECT buffers are faster to - // send but more expensive to build and access! so we have choices to make... - // + headers are constructed bit by bit and parsed bit by bit, so INDiRECT looks - // good for them. - // + but will a gather write of an INDIRECT header with a DIRECT body be any good? - // this needs to be benchmarked. - // + Will it be possible to get a DIRECT header buffer just for the gather writes of - // content from file mapped buffers? - // + Are gather writes worth the effort? Maybe they will work well with two INDIRECT - // buffers being copied into a single kernel buffer? - // - Buffer buf = null; - if (size==getHeaderBufferSize()) - buf= new IndirectNIOBuffer(size); - else - buf = _useDirectBuffers - ?(NIOBuffer)new DirectNIOBuffer(size) - :(NIOBuffer)new IndirectNIOBuffer(size); - return buf; - } + // TODO + // Header buffers always byte array buffers (efficiency of random access) + // There are lots of things to consider here... DIRECT buffers are faster to + // send but more expensive to build and access! so we have choices to make... + // + headers are constructed bit by bit and parsed bit by bit, so INDiRECT looks + // good for them. + // + but will a gather write of an INDIRECT header with a DIRECT body be any good? + // this needs to be benchmarked. + // + Will it be possible to get a DIRECT header buffer just for the gather writes of + // content from file mapped buffers? + // + Are gather writes worth the effort? Maybe they will work well with two INDIRECT + // buffers being copied into a single kernel buffer? + + /* ------------------------------------------------------------------------------- */ + public Buffer newRequestBuffer(int size) + { + return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newRequestHeader(int size) + { + return new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newResponseBuffer(int size) + { + return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size); + } + + /* ------------------------------------------------------------------------------- */ + public Buffer newResponseHeader(int size) + { + // TODO maybe can be direct? + return new IndirectNIOBuffer(size); + } + } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/nio/BlockingChannelConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/nio/BlockingChannelConnector.java index 28a6aeb8a9e..d167e43ce51 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/nio/BlockingChannelConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/nio/BlockingChannelConnector.java @@ -20,9 +20,9 @@ import java.nio.channels.ByteChannel; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import org.eclipse.jetty.http.HttpException; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EofException; -import org.eclipse.jetty.io.HttpException; import org.eclipse.jetty.io.nio.ChannelEndPoint; import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java index 5390b8ad358..bc7f55a0495 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ssl/SslSelectChannelConnector.java @@ -44,8 +44,10 @@ import org.eclipse.jetty.http.HttpSchemes; import org.eclipse.jetty.http.security.Password; import org.eclipse.jetty.http.ssl.SslSelectChannelEndPoint; import org.eclipse.jetty.io.Buffer; +import org.eclipse.jetty.io.Buffers; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.ThreadLocalBuffers; import org.eclipse.jetty.io.bio.SocketEndPoint; import org.eclipse.jetty.io.nio.DirectNIOBuffer; import org.eclipse.jetty.io.nio.IndirectNIOBuffer; @@ -114,61 +116,8 @@ public class SslSelectChannelConnector extends SelectChannelConnector private String _truststore; private String _truststoreType="JKS"; // type of the key store private SSLContext _context; + Buffers _sslBuffers; - private int _packetBufferSize; - private int _applicationBufferSize; - private ConcurrentLinkedQueue _packetBuffers = new ConcurrentLinkedQueue(); - private ConcurrentLinkedQueue _applicationBuffers = new ConcurrentLinkedQueue(); - - /* ------------------------------------------------------------ */ - /* (non-Javadoc) - * @see org.eclipse.jetty.io.AbstractBuffers#getBuffer(int) - */ - public Buffer getBuffer(int size) - { - // TODO why is this reimplemented? - Buffer buffer; - if (size==_applicationBufferSize) - { - buffer = _applicationBuffers.poll(); - if (buffer==null) - buffer=new IndirectNIOBuffer(size); - } - else if (size==_packetBufferSize) - { - buffer = _packetBuffers.poll(); - if (buffer==null) - buffer=getUseDirectBuffers() - ?(NIOBuffer)new DirectNIOBuffer(size) - :(NIOBuffer)new IndirectNIOBuffer(size); - } - else - buffer=super.getBuffer(size); - - return buffer; - } - - /* ------------------------------------------------------------ */ - /* (non-Javadoc) - * @see org.eclipse.jetty.io.AbstractBuffers#returnBuffer(org.eclipse.io.Buffer) - */ - public void returnBuffer(Buffer buffer) - { - buffer.clear(); - int size=buffer.capacity(); - ByteBuffer bbuf = ((NIOBuffer)buffer).getByteBuffer(); - bbuf.position(0); - bbuf.limit(size); - - if (size==_applicationBufferSize) - _applicationBuffers.add(buffer); - else if (size==_packetBufferSize) - _packetBuffers.add(buffer); - else - super.returnBuffer(buffer); - } - - /** * Return the chain of X509 certificates used to negotiate the SSL Session. @@ -528,7 +477,7 @@ public class SslSelectChannelConnector extends SelectChannelConnector /* ------------------------------------------------------------------------------- */ protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key) throws IOException { - return new SslSelectChannelEndPoint(this,channel,selectSet,key,createSSLEngine()); + return new SslSelectChannelEndPoint(_sslBuffers,channel,selectSet,key,createSSLEngine()); } /* ------------------------------------------------------------------------------- */ @@ -591,9 +540,29 @@ public class SslSelectChannelConnector extends SelectChannelConnector SSLEngine engine=createSSLEngine(); SSLSession ssl_session=engine.getSession(); - setHeaderBufferSize(ssl_session.getApplicationBufferSize()); - setRequestBufferSize(ssl_session.getApplicationBufferSize()); - setResponseBufferSize(ssl_session.getApplicationBufferSize()); + ThreadLocalBuffers buffers = new ThreadLocalBuffers() + { + @Override + protected Buffer newBuffer(int size) + { + // TODO indirect? + return new DirectNIOBuffer(size); + } + @Override + protected Buffer newHeader(int size) + { + // TODO indirect? + return new DirectNIOBuffer(size); + } + }; + buffers.setBufferSize(ssl_session.getApplicationBufferSize()); + buffers.setHeaderSize(ssl_session.getPacketBufferSize()); + _sslBuffers=buffers; + + if (getRequestHeaderSize()