Author: markt Date: Tue Aug 16 16:50:17 2011 New Revision: 1158367 URL: http://svn.apache.org/viewvc?rev=1158367&view=rev Log: Merge HTTP connector refactoring from trunk
Modified: tomcat/tc7.0.x/trunk/ (props changed) tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractInputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11Processor.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalInputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioInputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalOutputBuffer.java tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java tomcat/tc7.0.x/trunk/test/org/apache/coyote/http11/TestGzipOutputFilter.java tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Propchange: tomcat/tc7.0.x/trunk/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Tue Aug 16 16:50:17 2011 @@ -1 +1 @@ -/tomcat/trunk:1156171,1156276,1156304,1156530,1156602,1157015,1157018,1157810,1157832,1157834,1157847,1157908,1157939,1158155,1158176 +/tomcat/trunk:1156171,1156276,1156304,1156530,1156602,1157015,1157018,1157151,1157198,1157204,1157810,1157832,1157834,1157847,1157908,1157939,1158155,1158160,1158176,1158195,1158198-1158199,1158227,1158331,1158334-1158335 Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/ajp/AjpNioProcessor.java Tue Aug 16 16:50:17 2011 @@ -101,7 +101,6 @@ public class AjpNioProcessor extends Abs this.socket = socket.getSocket(); long soTimeout = endpoint.getSoTimeout(); - int keepAliveTimeout = endpoint.getKeepAliveTimeout(); // Error flag error = false; Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java Tue Aug 16 16:50:17 2011 @@ -612,14 +612,14 @@ public abstract class AbstractHttp11Proc * Exposes input buffer to super class to allow better code re-use. * @return The input buffer used by the processor. */ - protected abstract AbstractInputBuffer getInputBuffer(); + protected abstract AbstractInputBuffer<S> getInputBuffer(); /** * Exposes output buffer to super class to allow better code re-use. * @return The output buffer used by the processor. */ - protected abstract AbstractOutputBuffer getOutputBuffer(); + protected abstract AbstractOutputBuffer<S> getOutputBuffer(); /** @@ -747,7 +747,7 @@ public abstract class AbstractHttp11Proc InputFilter savedBody = new SavedRequestInputFilter(body); savedBody.setRequest(request); - AbstractInputBuffer internalBuffer = (AbstractInputBuffer) + AbstractInputBuffer<S> internalBuffer = (AbstractInputBuffer<S>) request.getInputBuffer(); internalBuffer.addActiveFilter(savedBody); } else if (actionCode == ActionCode.ASYNC_START) { @@ -778,6 +778,14 @@ public abstract class AbstractHttp11Proc /** + * Processors (currently only HTTP BIO) may elect to disable HTTP keep-alive + * in some circumstances. This method allows the processor implementation to + * determine if keep-alive should be disabled or not. + */ + protected abstract boolean disableKeepAlive(); + + + /** * After reading the request headers, we have to setup the request filters. */ protected void prepareRequest() { Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractInputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractInputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractInputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractInputBuffer.java Tue Aug 16 16:50:17 2011 @@ -17,15 +17,16 @@ package org.apache.coyote.http11; import java.io.IOException; -import java.io.InputStream; import org.apache.coyote.InputBuffer; import org.apache.coyote.Request; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.http.MimeHeaders; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; import org.apache.tomcat.util.res.StringManager; -public abstract class AbstractInputBuffer implements InputBuffer{ +public abstract class AbstractInputBuffer<S> implements InputBuffer{ protected static final boolean[] HTTP_TOKEN_CHAR = new boolean[128]; @@ -137,12 +138,6 @@ public abstract class AbstractInputBuffe /** - * Underlying input stream. - */ - protected InputStream inputStream; - - - /** * Underlying input buffer. */ protected InputBuffer inputStreamInputBuffer; @@ -171,28 +166,6 @@ public abstract class AbstractInputBuffe /** - * Set the underlying socket input stream. - */ - public void setInputStream(InputStream inputStream) { - - // FIXME: Check for null ? - - this.inputStream = inputStream; - - } - - - /** - * Get the underlying socket input stream. - */ - public InputStream getInputStream() { - - return inputStream; - - } - - - /** * Add an input filter to the filter library. */ public void addFilter(InputFilter filter) { @@ -252,12 +225,16 @@ public abstract class AbstractInputBuffe } - public abstract boolean parseRequestLine(boolean useAvailableDataOnly) throws IOException; + public abstract boolean parseRequestLine(boolean useAvailableDataOnly) + throws IOException; public abstract boolean parseHeaders() throws IOException; protected abstract boolean fill(boolean block) throws IOException; + protected abstract void init(SocketWrapper<S> socketWrapper, + AbstractEndpoint endpoint) throws IOException; + // --------------------------------------------------------- Public Methods @@ -271,7 +248,6 @@ public abstract class AbstractInputBuffe // Recycle Request object request.recycle(); - inputStream = null; lastValid = 0; pos = 0; lastActiveFilter = -1; Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/AbstractOutputBuffer.java Tue Aug 16 16:50:17 2011 @@ -28,9 +28,11 @@ import org.apache.tomcat.util.buf.ByteCh import org.apache.tomcat.util.buf.CharChunk; import org.apache.tomcat.util.buf.MessageBytes; import org.apache.tomcat.util.http.HttpMessages; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; import org.apache.tomcat.util.res.StringManager; -public abstract class AbstractOutputBuffer implements OutputBuffer{ +public abstract class AbstractOutputBuffer<S> implements OutputBuffer{ // ----------------------------------------------------- Instance Variables @@ -316,7 +318,11 @@ public abstract class AbstractOutputBuff activeFilters[lastActiveFilter].end(); finished = true; } + + public abstract void init(SocketWrapper<S> socketWrapper, + AbstractEndpoint endpoint) throws IOException; + public abstract void sendAck() throws IOException; protected abstract void commit() throws IOException; Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java Tue Aug 16 16:50:17 2011 @@ -174,22 +174,26 @@ public class Http11AprProcessor extends // Setting up the socket this.socket = socketWrapper; - long socketRef = socketWrapper.getSocket().longValue(); - inputBuffer.setSocket(socketRef); - outputBuffer.setSocket(socketRef); + inputBuffer.init(socketWrapper, endpoint); + outputBuffer.init(socketWrapper, endpoint); // Error flag error = false; keepAlive = true; comet = false; - int keepAliveLeft = maxKeepAliveRequests; - long soTimeout = endpoint.getSoTimeout(); - + int soTimeout = endpoint.getSoTimeout(); + + if (disableKeepAlive()) { + socketWrapper.setKeepAliveLeft(0); + } + boolean keptAlive = false; boolean openSocket = false; boolean sendfileInProgress = false; + long socketRef = socketWrapper.getSocket().longValue(); + while (!error && keepAlive && !comet && !isAsync() && !endpoint.isPaused()) { // Parsing the request header @@ -251,8 +255,12 @@ public class Http11AprProcessor extends } } - if (maxKeepAliveRequests > 0 && --keepAliveLeft == 0) + if (maxKeepAliveRequests == 1) { keepAlive = false; + } else if (maxKeepAliveRequests > 0 && + socketWrapper.decrementKeepAlive() <= 0) { + keepAlive = false; + } // Process the request in the adapter if (!error) { @@ -349,6 +357,12 @@ public class Http11AprProcessor extends @Override + protected boolean disableKeepAlive() { + return false; + } + + + @Override protected void resetTimeouts() { // NOOP for APR } @@ -619,12 +633,12 @@ public class Http11AprProcessor extends } @Override - protected AbstractInputBuffer getInputBuffer() { + protected AbstractInputBuffer<Long> getInputBuffer() { return inputBuffer; } @Override - protected AbstractOutputBuffer getOutputBuffer() { + protected AbstractOutputBuffer<Long> getOutputBuffer() { return outputBuffer; } } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java Tue Aug 16 16:50:17 2011 @@ -247,6 +247,7 @@ public class Http11AprProtocol extends A proto.getMaxTrailerSize()); processor.setAdapter(proto.adapter); processor.setMaxKeepAliveRequests(proto.getMaxKeepAliveRequests()); + processor.setKeepAliveTimeout(proto.getKeepAliveTimeout()); processor.setConnectionUploadTimeout( proto.getConnectionUploadTimeout()); processor.setDisableUploadTimeout(proto.getDisableUploadTimeout()); Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java Tue Aug 16 16:50:17 2011 @@ -123,7 +123,6 @@ public class Http11NioProcessor extends throws IOException { long soTimeout = endpoint.getSoTimeout(); - int keepAliveTimeout = endpoint.getKeepAliveTimeout(); RequestInfo rp = request.getRequestProcessor(); final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socket.getSocket().getAttachment(false); @@ -176,7 +175,6 @@ public class Http11NioProcessor extends if (!error && attach != null && asyncStateMachine.isAsyncDispatching()) { long soTimeout = endpoint.getSoTimeout(); - int keepAliveTimeout = endpoint.getKeepAliveTimeout(); //reset the timeout if (keepAlive && keepAliveTimeout>0) { @@ -205,18 +203,19 @@ public class Http11NioProcessor extends // Setting up the socket this.socket = socketWrapper; - inputBuffer.setSocket(this.socket.getSocket()); - outputBuffer.setSocket(this.socket.getSocket()); - inputBuffer.setSelectorPool(((NioEndpoint)endpoint).getSelectorPool()); - outputBuffer.setSelectorPool(((NioEndpoint)endpoint).getSelectorPool()); + inputBuffer.init(socketWrapper, endpoint); + outputBuffer.init(socketWrapper, endpoint); // Error flag error = false; keepAlive = true; comet = false; - long soTimeout = endpoint.getSoTimeout(); - int keepAliveTimeout = endpoint.getKeepAliveTimeout(); + int soTimeout = endpoint.getSoTimeout(); + + if (disableKeepAlive()) { + socketWrapper.setKeepAliveLeft(0); + } boolean keptAlive = false; boolean openSocket = false; @@ -228,7 +227,7 @@ public class Http11NioProcessor extends // Parsing the request header try { if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) { - socketWrapper.getSocket().getIOChannel().socket().setSoTimeout((int)soTimeout); + socketWrapper.getSocket().getIOChannel().socket().setSoTimeout(soTimeout); } if (!inputBuffer.parseRequestLine(keptAlive)) { // Haven't finished reading the request so keep the socket @@ -304,10 +303,12 @@ public class Http11NioProcessor extends } } - if (maxKeepAliveRequests == 1 ) + if (maxKeepAliveRequests == 1) { keepAlive = false; - if (maxKeepAliveRequests > 0 && socketWrapper.decrementKeepAlive() <= 0) + } else if (maxKeepAliveRequests > 0 && + socketWrapper.decrementKeepAlive() <= 0) { keepAlive = false; + } // Process the request in the adapter if (!error) { @@ -402,6 +403,12 @@ public class Http11NioProcessor extends @Override + protected boolean disableKeepAlive() { + return false; + } + + + @Override public void recycleInternal() { socket = null; cometClose = false; @@ -647,12 +654,12 @@ public class Http11NioProcessor extends } @Override - protected AbstractInputBuffer getInputBuffer() { + protected AbstractInputBuffer<NioChannel> getInputBuffer() { return inputBuffer; } @Override - protected AbstractOutputBuffer getOutputBuffer() { + protected AbstractOutputBuffer<NioChannel> getOutputBuffer() { return outputBuffer; } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11NioProtocol.java Tue Aug 16 16:50:17 2011 @@ -277,6 +277,7 @@ public class Http11NioProtocol extends A proto.getMaxTrailerSize()); processor.setAdapter(proto.adapter); processor.setMaxKeepAliveRequests(proto.getMaxKeepAliveRequests()); + processor.setKeepAliveTimeout(proto.getKeepAliveTimeout()); processor.setConnectionUploadTimeout( proto.getConnectionUploadTimeout()); processor.setDisableUploadTimeout(proto.getDisableUploadTimeout()); Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11Processor.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11Processor.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11Processor.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/Http11Processor.java Tue Aug 16 16:50:17 2011 @@ -138,30 +138,17 @@ public class Http11Processor extends Abs // Setting up the I/O this.socket = socketWrapper; - inputBuffer.setInputStream(socket.getSocket().getInputStream()); - outputBuffer.setOutputStream(socket.getSocket().getOutputStream()); + inputBuffer.init(socketWrapper, endpoint); + outputBuffer.init(socketWrapper, endpoint); // Error flag error = false; keepAlive = true; + comet = false; - if (maxKeepAliveRequests > 0) { - socketWrapper.decrementKeepAlive(); - } - int soTimeout = endpoint.getSoTimeout(); - int threadRatio = -1; - // These may return zero or negative values - // Only calculate a thread ratio when both are >0 to ensure we get a - // sensible result - if (endpoint.getCurrentThreadsBusy() >0 && - endpoint.getMaxThreads() >0) { - threadRatio = (endpoint.getCurrentThreadsBusy() * 100) - / endpoint.getMaxThreads(); - } - // Disable keep-alive if we are running low on threads - if (threadRatio > getDisableKeepAlivePercentage()) { + if (disableKeepAlive()) { socketWrapper.setKeepAliveLeft(0); } @@ -268,7 +255,10 @@ public class Http11Processor extends Abs } } - if (socketWrapper.getKeepAliveLeft() == 0) { + if (maxKeepAliveRequests == 1) { + keepAlive = false; + } else if (maxKeepAliveRequests > 0 && + socketWrapper.decrementKeepAlive() <= 0) { keepAlive = false; } @@ -350,10 +340,6 @@ public class Http11Processor extends Abs if (isAsync() || error || inputBuffer.lastValid == 0) { break; } - - if (maxKeepAliveRequests > 0) { - socketWrapper.decrementKeepAlive(); - } } rp.setStage(org.apache.coyote.Constants.STAGE_ENDED); @@ -372,6 +358,26 @@ public class Http11Processor extends Abs @Override + protected boolean disableKeepAlive() { + int threadRatio = -1; + // These may return zero or negative values + // Only calculate a thread ratio when both are >0 to ensure we get a + // sensible result + if (endpoint.getCurrentThreadsBusy() >0 && + endpoint.getMaxThreads() >0) { + threadRatio = (endpoint.getCurrentThreadsBusy() * 100) + / endpoint.getMaxThreads(); + } + // Disable keep-alive if we are running low on threads + if (threadRatio > getDisableKeepAlivePercentage()) { + return true; + } + + return false; + } + + + @Override protected void resetTimeouts() { // NOOP for APR } @@ -558,12 +564,12 @@ public class Http11Processor extends Abs } @Override - protected AbstractInputBuffer getInputBuffer() { + protected AbstractInputBuffer<Socket> getInputBuffer() { return inputBuffer; } @Override - protected AbstractOutputBuffer getOutputBuffer() { + protected AbstractOutputBuffer<Socket> getOutputBuffer() { return outputBuffer; } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprInputBuffer.java Tue Aug 16 16:50:17 2011 @@ -30,6 +30,8 @@ import org.apache.tomcat.jni.Socket; import org.apache.tomcat.jni.Status; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; /** * Implementation of InputBuffer which provides HTTP request header parsing as @@ -37,7 +39,7 @@ import org.apache.tomcat.util.buf.Messag * * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> */ -public class InternalAprInputBuffer extends AbstractInputBuffer { +public class InternalAprInputBuffer extends AbstractInputBuffer<Long> { private static final Log log = LogFactory.getLog(InternalAprInputBuffer.class); @@ -78,38 +80,17 @@ public class InternalAprInputBuffer exte /** * Direct byte buffer used to perform actual reading. */ - protected ByteBuffer bbuf; + private ByteBuffer bbuf; /** * Underlying socket. */ - protected long socket; - - - // ------------------------------------------------------------- Properties - - - /** - * Set the underlying socket. - */ - public void setSocket(long socket) { - this.socket = socket; - Socket.setrbb(this.socket, bbuf); - } - - - /** - * Get the underlying socket input stream. - */ - public long getSocket() { - return socket; - } + private long socket; // --------------------------------------------------------- Public Methods - /** * Recycle the input buffer. This should be called when closing the * connection. @@ -341,7 +322,7 @@ public class InternalAprInputBuffer exte * HTTP header parsing is done */ @SuppressWarnings("null") // headerValue cannot be null - public boolean parseHeader() + private boolean parseHeader() throws IOException { // @@ -567,6 +548,14 @@ public class InternalAprInputBuffer exte // ------------------------------------------------------ Protected Methods + @Override + protected void init(SocketWrapper<Long> socketWrapper, + AbstractEndpoint endpoint) throws IOException { + + socket = socketWrapper.getSocket().longValue(); + Socket.setrbb(this.socket, bbuf); + } + @Override protected boolean fill(boolean block) throws IOException { @@ -666,11 +655,6 @@ public class InternalAprInputBuffer exte pos = lastValid; return (length); - } - - } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalAprOutputBuffer.java Tue Aug 16 16:50:17 2011 @@ -26,13 +26,15 @@ import org.apache.coyote.Response; import org.apache.tomcat.jni.Socket; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.http.HttpMessages; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; /** * Output buffer. * * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> */ -public class InternalAprOutputBuffer extends AbstractOutputBuffer { +public class InternalAprOutputBuffer extends AbstractOutputBuffer<Long> { // ----------------------------------------------------------- Constructors @@ -72,30 +74,26 @@ public class InternalAprOutputBuffer ext /** * Underlying socket. */ - protected long socket; + private long socket; /** * Direct byte buffer used for writing. */ - protected ByteBuffer bbuf = null; + private ByteBuffer bbuf = null; - // ------------------------------------------------------------- Properties + // --------------------------------------------------------- Public Methods + @Override + public void init(SocketWrapper<Long> socketWrapper, + AbstractEndpoint endpoint) throws IOException { - /** - * Set the underlying socket. - */ - public void setSocket(long socket) { - this.socket = socket; + socket = socketWrapper.getSocket().longValue(); Socket.setsbb(this.socket, bbuf); } - // --------------------------------------------------------- Public Methods - - /** * Flush the response. * @@ -197,11 +195,10 @@ public class InternalAprOutputBuffer ext } - /** * Callback to write data from the buffer. */ - protected void flushBuffer() + private void flushBuffer() throws IOException { if (bbuf.position() > 0) { if (Socket.sendbb(socket, 0, bbuf.position()) < 0) { @@ -253,6 +250,4 @@ public class InternalAprOutputBuffer ext return byteCount; } } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalInputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalInputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalInputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalInputBuffer.java Tue Aug 16 16:50:17 2011 @@ -18,6 +18,8 @@ package org.apache.coyote.http11; import java.io.EOFException; import java.io.IOException; +import java.io.InputStream; +import java.net.Socket; import java.nio.charset.Charset; import org.apache.coyote.InputBuffer; @@ -26,6 +28,8 @@ import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; /** * Implementation of InputBuffer which provides HTTP request header parsing as @@ -33,12 +37,18 @@ import org.apache.tomcat.util.buf.Messag * * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> */ -public class InternalInputBuffer extends AbstractInputBuffer { +public class InternalInputBuffer extends AbstractInputBuffer<Socket> { private static final Log log = LogFactory.getLog(InternalInputBuffer.class); /** + * Underlying input stream. + */ + private InputStream inputStream; + + + /** * Default constructor. */ public InternalInputBuffer(Request request, int headerBufferSize) { @@ -59,6 +69,7 @@ public class InternalInputBuffer extends } + /** * Read the request line. This function is meant to be used during the * HTTP request header parsing. Do NOT attempt to read the request body @@ -267,7 +278,7 @@ public class InternalInputBuffer extends * HTTP header parsing is done */ @SuppressWarnings("null") // headerValue cannot be null - public boolean parseHeader() + private boolean parseHeader() throws IOException { // @@ -428,9 +439,24 @@ public class InternalInputBuffer extends } + @Override + public void recycle() { + super.recycle(); + inputStream = null; + } + + // ------------------------------------------------------ Protected Methods + @Override + protected void init(SocketWrapper<Socket> socketWrapper, + AbstractEndpoint endpoint) throws IOException { + inputStream = socketWrapper.getSocket().getInputStream(); + } + + + private void skipLine(int start) throws IOException { boolean eol = false; int lastRealByte = start; @@ -539,11 +565,6 @@ public class InternalInputBuffer extends pos = lastValid; return (length); - } - - } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioInputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioInputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioInputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioInputBuffer.java Tue Aug 16 16:50:17 2011 @@ -25,9 +25,11 @@ import org.apache.coyote.InputBuffer; import org.apache.coyote.Request; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.MessageBytes; +import org.apache.tomcat.util.net.AbstractEndpoint; import org.apache.tomcat.util.net.NioChannel; import org.apache.tomcat.util.net.NioEndpoint; import org.apache.tomcat.util.net.NioSelectorPool; +import org.apache.tomcat.util.net.SocketWrapper; /** * Implementation of InputBuffer which provides HTTP request header parsing as @@ -36,7 +38,7 @@ import org.apache.tomcat.util.net.NioSel * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> * @author Filip Hanik */ -public class InternalNioInputBuffer extends AbstractInputBuffer { +public class InternalNioInputBuffer extends AbstractInputBuffer<NioChannel> { private static final org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory.getLog(InternalNioInputBuffer.class); @@ -84,22 +86,22 @@ public class InternalNioInputBuffer exte * Parsing state - used for non blocking parsing so that * when more data arrives, we can pick up where we left off. */ - protected boolean parsingRequestLine; - protected int parsingRequestLinePhase = 0; - protected boolean parsingRequestLineEol = false; - protected int parsingRequestLineStart = 0; - protected int parsingRequestLineQPos = -1; - protected HeaderParsePosition headerParsePos; + private boolean parsingRequestLine; + private int parsingRequestLinePhase = 0; + private boolean parsingRequestLineEol = false; + private int parsingRequestLineStart = 0; + private int parsingRequestLineQPos = -1; + private HeaderParsePosition headerParsePos; /** * Underlying socket. */ - protected NioChannel socket; + private NioChannel socket; /** * Selector pool, for blocking reads and blocking writes */ - protected NioSelectorPool pool; + private NioSelectorPool pool; /** @@ -124,47 +126,8 @@ public class InternalNioInputBuffer exte */ private int skipBlankLinesBytes; - // ------------------------------------------------------------- Properties - - - /** - * Set the underlying socket. - */ - public void setSocket(NioChannel socket) { - this.socket = socket; - socketReadBufferSize = socket.getBufHandler().getReadBuffer().capacity(); - int bufLength = skipBlankLinesSize + headerBufferSize - + socketReadBufferSize; - if (buf == null || buf.length < bufLength) { - buf = new byte[bufLength]; - } - } - - /** - * Get the underlying socket input stream. - */ - public NioChannel getSocket() { - return socket; - } - - public void setSelectorPool(NioSelectorPool pool) { - this.pool = pool; - } - - public NioSelectorPool getSelectorPool() { - return pool; - } - // --------------------------------------------------------- Public Methods - /** - * Issues a non blocking read - * @return int - * @throws IOException - */ - public int nbRead() throws IOException { - return readSocket(true,false); - } /** * Recycle the input buffer. This should be called when closing the @@ -429,18 +392,18 @@ public class InternalNioInputBuffer exte if ( block ) { Selector selector = null; try { - selector = getSelectorPool().get(); + selector = pool.get(); } catch ( IOException x ) { // Ignore } try { NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false); if ( att == null ) throw new IOException("Key must be cancelled."); - nRead = getSelectorPool().read(socket.getBufHandler().getReadBuffer(),socket,selector,att.getTimeout()); + nRead = pool.read(socket.getBufHandler().getReadBuffer(),socket,selector,att.getTimeout()); } catch ( EOFException eof ) { nRead = -1; } finally { - if ( selector != null ) getSelectorPool().put(selector); + if ( selector != null ) pool.put(selector); } } else { nRead = socket.read(socket.getBufHandler().getReadBuffer()); @@ -500,7 +463,7 @@ public class InternalNioInputBuffer exte * @return false after reading a blank line (which indicates that the * HTTP header parsing is done */ - public HeaderParseStatus parseHeader() + private HeaderParseStatus parseHeader() throws IOException { // @@ -677,6 +640,10 @@ public class InternalNioInputBuffer exte return HeaderParseStatus.HAVE_MORE_HEADERS; } + public int getParsingRequestLinePhase() { + return parsingRequestLinePhase; + } + private HeaderParseStatus skipLine() throws IOException { headerParsePos = HeaderParsePosition.HEADER_SKIPLINE; boolean eol = false; @@ -712,7 +679,7 @@ public class InternalNioInputBuffer exte return HeaderParseStatus.HAVE_MORE_HEADERS; } - protected HeaderParseData headerData = new HeaderParseData(); + private HeaderParseData headerData = new HeaderParseData(); public static class HeaderParseData { int start = 0; int realPos = 0; @@ -743,6 +710,24 @@ public class InternalNioInputBuffer exte // ------------------------------------------------------ Protected Methods + @Override + protected void init(SocketWrapper<NioChannel> socketWrapper, + AbstractEndpoint endpoint) throws IOException { + + socket = socketWrapper.getSocket(); + socketReadBufferSize = + socket.getBufHandler().getReadBuffer().capacity(); + + int bufLength = skipBlankLinesSize + headerBufferSize + + socketReadBufferSize; + if (buf == null || buf.length < bufLength) { + buf = new byte[bufLength]; + } + + pool = ((NioEndpoint)endpoint).getSelectorPool(); + } + + /** * Fill the internal buffer using data from the underlying input stream. * @@ -804,16 +789,6 @@ public class InternalNioInputBuffer exte pos = lastValid; return (length); - } - - - } - - - public int getParsingRequestLinePhase() { - return parsingRequestLinePhase; } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java Tue Aug 16 16:50:17 2011 @@ -27,9 +27,11 @@ import org.apache.coyote.Response; import org.apache.tomcat.util.MutableInteger; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.http.HttpMessages; +import org.apache.tomcat.util.net.AbstractEndpoint; import org.apache.tomcat.util.net.NioChannel; import org.apache.tomcat.util.net.NioEndpoint; import org.apache.tomcat.util.net.NioSelectorPool; +import org.apache.tomcat.util.net.SocketWrapper; /** * Output buffer. @@ -37,7 +39,7 @@ import org.apache.tomcat.util.net.NioSel * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> * @author Filip Hanik */ -public class InternalNioOutputBuffer extends AbstractOutputBuffer { +public class InternalNioOutputBuffer extends AbstractOutputBuffer<NioChannel> { // ----------------------------------------------------------- Constructors @@ -68,37 +70,19 @@ public class InternalNioOutputBuffer ext /** * Number of bytes last written */ - protected MutableInteger lastWrite = new MutableInteger(1); + private MutableInteger lastWrite = new MutableInteger(1); /** * Underlying socket. */ - protected NioChannel socket; + private NioChannel socket; /** * Selector pool, for blocking reads and blocking writes */ - protected NioSelectorPool pool; + private NioSelectorPool pool; - // ------------------------------------------------------------- Properties - - - /** - * Set the underlying socket. - */ - public void setSocket(NioChannel socket) { - this.socket = socket; - } - - public void setSelectorPool(NioSelectorPool pool) { - this.pool = pool; - } - - public NioSelectorPool getSelectorPool() { - return pool; - } - // --------------------------------------------------------- Public Methods @@ -178,18 +162,18 @@ public class InternalNioOutputBuffer ext long writeTimeout = att.getTimeout(); Selector selector = null; try { - selector = getSelectorPool().get(); + selector = pool.get(); } catch ( IOException x ) { //ignore } try { - written = getSelectorPool().write(bytebuffer, socket, selector, writeTimeout, block,lastWrite); + written = pool.write(bytebuffer, socket, selector, writeTimeout, block,lastWrite); //make sure we are flushed do { if (socket.flush(true,selector,writeTimeout,lastWrite)) break; }while ( true ); }finally { - if ( selector != null ) getSelectorPool().put(selector); + if ( selector != null ) pool.put(selector); } if ( block ) bytebuffer.clear(); //only clear this.total = 0; @@ -199,6 +183,14 @@ public class InternalNioOutputBuffer ext // ------------------------------------------------------ Protected Methods + @Override + public void init(SocketWrapper<NioChannel> socketWrapper, + AbstractEndpoint endpoint) throws IOException { + + socket = socketWrapper.getSocket(); + pool = ((NioEndpoint)endpoint).getSelectorPool(); + } + /** * Commit the response. @@ -220,7 +212,7 @@ public class InternalNioOutputBuffer ext } - int total = 0; + private int total = 0; private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException { while (length > 0) { int thisTime = length; @@ -245,8 +237,7 @@ public class InternalNioOutputBuffer ext /** * Callback to write data from the buffer. */ - protected void flushBuffer() - throws IOException { + private void flushBuffer() throws IOException { //prevent timeout for async, SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector()); @@ -294,6 +285,4 @@ public class InternalNioOutputBuffer ext return byteCount; } } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalOutputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalOutputBuffer.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalOutputBuffer.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/coyote/http11/InternalOutputBuffer.java Tue Aug 16 16:50:17 2011 @@ -19,17 +19,20 @@ package org.apache.coyote.http11; import java.io.IOException; import java.io.OutputStream; +import java.net.Socket; import org.apache.coyote.OutputBuffer; import org.apache.coyote.Response; import org.apache.tomcat.util.buf.ByteChunk; +import org.apache.tomcat.util.net.AbstractEndpoint; +import org.apache.tomcat.util.net.SocketWrapper; /** * Output buffer. * * @author <a href="mailto:r...@apache.org">Remy Maucherat</a> */ -public class InternalOutputBuffer extends AbstractOutputBuffer +public class InternalOutputBuffer extends AbstractOutputBuffer<Socket> implements ByteChunk.ByteOutputChannel { // ----------------------------------------------------------- Constructors @@ -58,7 +61,7 @@ public class InternalOutputBuffer extend } /** - * Underlying output stream. + * Underlying output stream. Note: protected to assist with unit testing */ protected OutputStream outputStream; @@ -66,28 +69,16 @@ public class InternalOutputBuffer extend /** * Socket buffer. */ - protected ByteChunk socketBuffer; + private ByteChunk socketBuffer; /** * Socket buffer (extra buffering to reduce number of packets sent). */ - protected boolean useSocketBuffer = false; + private boolean useSocketBuffer = false; /** - * Set the underlying socket output stream. - */ - public void setOutputStream(OutputStream outputStream) { - - // FIXME: Check for null ? - - this.outputStream = outputStream; - - } - - - /** * Set the socket buffer size. */ public void setSocketBuffer(int socketBufferSize) { @@ -104,6 +95,13 @@ public class InternalOutputBuffer extend // --------------------------------------------------------- Public Methods + @Override + public void init(SocketWrapper<Socket> socketWrapper, + AbstractEndpoint endpoint) throws IOException { + + outputStream = socketWrapper.getSocket().getOutputStream(); + } + /** * Flush the response. @@ -255,6 +253,4 @@ public class InternalOutputBuffer extend return byteCount; } } - - } Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java Tue Aug 16 16:50:17 2011 @@ -45,7 +45,6 @@ public class SocketWrapper<E> { public long getTimeout() {return this.timeout;} public boolean getError() { return error; } public void setError(boolean error) { this.error = error; } - public int getKeepAliveLeft() { return this.keepAliveLeft; } public void setKeepAliveLeft(int keepAliveLeft) { this.keepAliveLeft = keepAliveLeft;} public int decrementKeepAlive() { return (--keepAliveLeft);} public boolean isKeptAlive() {return keptAlive;} Modified: tomcat/tc7.0.x/trunk/test/org/apache/coyote/http11/TestGzipOutputFilter.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/coyote/http11/TestGzipOutputFilter.java?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/test/org/apache/coyote/http11/TestGzipOutputFilter.java (original) +++ tomcat/tc7.0.x/trunk/test/org/apache/coyote/http11/TestGzipOutputFilter.java Tue Aug 16 16:50:17 2011 @@ -54,7 +54,7 @@ public class TestGzipOutputFilter { Response res = new Response(); InternalOutputBuffer iob = new InternalOutputBuffer(res, 8 * 1024); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - iob.setOutputStream(bos); + iob.outputStream = bos; res.setOutputBuffer(iob); // set up GzipOutputFilter to attach to the InternalOutputBuffer Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1158367&r1=1158366&r2=1158367&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Tue Aug 16 16:50:17 2011 @@ -97,6 +97,10 @@ <bug>51641</bug>: Use correct key when removing processor instances from the connections map during clean-up. Patch provided by zhh. (mark) </fix> + <fix> + More changes to align the code between the different HTTP connectors. + (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org