From 64be1e2d5d2e968a6b89828d3c7f46307d009c36 Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 26 Oct 2006 20:37:40 +0000 Subject: [PATCH] Make sure the socket buffer is not bigger than anticipated header size Reuse the key attachment objects properly git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@468124 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/coyote/http11/Http11NioProcessor.java | 6 +++--- .../apache/coyote/http11/Http11NioProtocol.java | 5 +++-- .../coyote/http11/InternalNioOutputBuffer.java | 2 +- .../apache/coyote/http11/InternalOutputBuffer.java | 1 + java/org/apache/tomcat/util/net/NioChannel.java | 12 ++++++++++-- java/org/apache/tomcat/util/net/NioEndpoint.java | 22 ++++++++++------------ 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/java/org/apache/coyote/http11/Http11NioProcessor.java b/java/org/apache/coyote/http11/Http11NioProcessor.java index 11e7057cf..1da6684d0 100644 --- a/java/org/apache/coyote/http11/Http11NioProcessor.java +++ b/java/org/apache/coyote/http11/Http11NioProcessor.java @@ -83,7 +83,7 @@ public class Http11NioProcessor implements ActionHook { // ----------------------------------------------------------- Constructors - public Http11NioProcessor(int rxBufSize, int txBufSize, NioEndpoint endpoint) { + public Http11NioProcessor(int rxBufSize, int txBufSize, int maxHttpHeaderSize, NioEndpoint endpoint) { this.endpoint = endpoint; @@ -95,12 +95,12 @@ public class Http11NioProcessor implements ActionHook { readTimeout = timeout; //readTimeout = -1; } - inputBuffer = new InternalNioInputBuffer(request, rxBufSize,readTimeout); + inputBuffer = new InternalNioInputBuffer(request, maxHttpHeaderSize,readTimeout); request.setInputBuffer(inputBuffer); response = new Response(); response.setHook(this); - outputBuffer = new InternalNioOutputBuffer(response, txBufSize,readTimeout); + outputBuffer = new InternalNioOutputBuffer(response, maxHttpHeaderSize,readTimeout); response.setOutputBuffer(outputBuffer); request.setResponse(response); diff --git a/java/org/apache/coyote/http11/Http11NioProtocol.java b/java/org/apache/coyote/http11/Http11NioProtocol.java index 2689ab6b9..d05bf8a4b 100644 --- a/java/org/apache/coyote/http11/Http11NioProtocol.java +++ b/java/org/apache/coyote/http11/Http11NioProtocol.java @@ -655,8 +655,9 @@ public class Http11NioProtocol implements ProtocolHandler, MBeanRegistration public Http11NioProcessor createProcessor() { Http11NioProcessor processor = new Http11NioProcessor( - Math.max(proto.maxHttpHeaderSize,proto.ep.getSocketProperties().getRxBufSize()), - Math.max(proto.maxHttpHeaderSize,proto.ep.getSocketProperties().getRxBufSize()), + proto.ep.getSocketProperties().getRxBufSize(), + proto.ep.getSocketProperties().getTxBufSize(), + proto.maxHttpHeaderSize, proto.ep); processor.setAdapter(proto.adapter); processor.setMaxKeepAliveRequests(proto.maxKeepAliveRequests); diff --git a/java/org/apache/coyote/http11/InternalNioOutputBuffer.java b/java/org/apache/coyote/http11/InternalNioOutputBuffer.java index fd625a180..206880fcd 100644 --- a/java/org/apache/coyote/http11/InternalNioOutputBuffer.java +++ b/java/org/apache/coyote/http11/InternalNioOutputBuffer.java @@ -605,7 +605,7 @@ public class InternalNioOutputBuffer int total = 0; private void addToBB(byte[] buf, int offset, int length) throws IOException { - if (socket.getBufHandler().getWriteBuffer().remaining() <= length) { + if (socket.getBufHandler().getWriteBuffer().remaining() < length) { flushBuffer(); } socket.getBufHandler().getWriteBuffer().put(buf, offset, length); diff --git a/java/org/apache/coyote/http11/InternalOutputBuffer.java b/java/org/apache/coyote/http11/InternalOutputBuffer.java index 4acce12a5..398e93513 100644 --- a/java/org/apache/coyote/http11/InternalOutputBuffer.java +++ b/java/org/apache/coyote/http11/InternalOutputBuffer.java @@ -61,6 +61,7 @@ public class InternalOutputBuffer public InternalOutputBuffer(Response response, int headerBufferSize) { this.response = response; + headers = response.getMimeHeaders(); headerBuffer = new byte[headerBufferSize]; diff --git a/java/org/apache/tomcat/util/net/NioChannel.java b/java/org/apache/tomcat/util/net/NioChannel.java index d28fc5631..ce341546e 100644 --- a/java/org/apache/tomcat/util/net/NioChannel.java +++ b/java/org/apache/tomcat/util/net/NioChannel.java @@ -26,6 +26,7 @@ import java.nio.channels.SocketChannel; import org.apache.tomcat.util.net.NioEndpoint.Poller; import org.apache.tomcat.util.net.SecureNioChannel.ApplicationBufferHandler; import java.nio.channels.Selector; +import java.nio.channels.SelectionKey; /** * @@ -82,7 +83,7 @@ public class NioChannel implements ByteChannel{ */ public void close() throws IOException { getIOChannel().socket().close(); - sc.close(); + getIOChannel().close(); } public void close(boolean force) throws IOException { @@ -122,7 +123,14 @@ public class NioChannel implements ByteChannel{ return sc.read(dst); } - + public Object getAttachment(boolean remove) { + Poller pol = getPoller(); + Selector sel = pol!=null?pol.getSelector():null; + SelectionKey key = sel!=null?getIOChannel().keyFor(sel):null; + Object att = key!=null?key.attachment():null; + if (key != null && att != null && remove ) key.attach(null); + return att; + } /** * getBufHandler * diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 1e296695a..4c7a4a581 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -165,16 +165,7 @@ public class NioEndpoint { protected ConcurrentLinkedQueue nioChannels = new ConcurrentLinkedQueue() { protected AtomicInteger size = new AtomicInteger(0); protected AtomicInteger bytes = new AtomicInteger(0); - public boolean offer(NioChannel socket) { - Poller pol = socket.getPoller(); - Selector sel = pol!=null?pol.getSelector():null; - SelectionKey key = sel!=null?socket.getIOChannel().keyFor(sel):null; - KeyAttachment att = key!=null?(KeyAttachment)key.attachment():null; - if ( att!=null ) { - att.reset(); - keyCache.offer(att); - } - if ( key!=null ) key.attach(null); + public boolean offer(NioChannel socket, KeyAttachment att) { boolean offer = socketProperties.getBufferPool()==-1?true:size.get()