import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.SocketStatus;
+import org.apache.tomcat.util.net.SocketWrapper;
import org.apache.tomcat.util.res.StringManager;
-public abstract class AbstractHttp11Processor extends AbstractProcessor {
+public abstract class AbstractHttp11Processor<S> extends AbstractProcessor {
protected abstract Log getLog();
}
+ public abstract SocketState process(SocketWrapper<S> socket)
+ throws IOException;
+
public SocketState asyncDispatch(SocketStatus status) {
RequestInfo rp = request.getRequestProcessor();
*/
package org.apache.coyote.http11;
+import java.util.concurrent.ConcurrentHashMap;
+
import org.apache.coyote.AbstractProtocol;
+import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.net.SocketStatus;
+import org.apache.tomcat.util.net.SocketWrapper;
import org.apache.tomcat.util.res.StringManager;
public abstract class AbstractHttp11Protocol extends AbstractProtocol {
public void setMaxKeepAliveRequests(int mkar) {
endpoint.setMaxKeepAliveRequests(mkar);
}
+
+
+ protected abstract static class AbstractHttp11ConnectionHandler<S,P extends AbstractHttp11Processor<S>>
+ extends AbstractConnectionHandler {
+
+ protected ConcurrentHashMap<SocketWrapper<S>,P> connections =
+ new ConcurrentHashMap<SocketWrapper<S>,P>();
+
+ protected RecycledProcessors<P> recycledProcessors =
+ new RecycledProcessors<P>(this);
+
+ @Override
+ public void recycle() {
+ recycledProcessors.clear();
+ }
+
+ public SocketState process(SocketWrapper<S> socket,
+ SocketStatus status) {
+ P processor = connections.remove(socket);
+
+ socket.setAsync(false); //no longer check for timeout
+
+ try {
+ if (processor == null) {
+ processor = recycledProcessors.poll();
+ }
+ if (processor == null) {
+ processor = createProcessor();
+ }
+
+ initSsl(socket, processor);
+
+ SocketState state = SocketState.CLOSED;
+ do {
+ if (processor.isAsync() || state == SocketState.ASYNC_END) {
+ state = processor.asyncDispatch(status);
+ } else if (processor.comet) {
+ state = processor.event(status);
+ } else {
+ state = processor.process(socket);
+ }
+
+ if (state != SocketState.CLOSED && processor.isAsync()) {
+ state = processor.asyncPostProcess();
+ }
+ } while (state == SocketState.ASYNC_END);
+
+ if (state == SocketState.LONG) {
+ // In the middle of processing a request/response. Keep the
+ // socket associated with the processor. Exact requirements
+ // depend on type of long poll
+ longPoll(socket, processor);
+ } else if (state == SocketState.OPEN){
+ // In keep-alive but between requests. OK to recycle
+ // processor. Continue to poll for the next request.
+ release(socket, processor, false, true);
+ } else {
+ // Connection closed. OK to recycle the processor.
+ release(socket, processor, true, false);
+ }
+ return state;
+ } catch(java.net.SocketException e) {
+ // SocketExceptions are normal
+ getLog().debug(sm.getString(
+ "http11protocol.proto.socketexception.debug"), e);
+ } catch (java.io.IOException e) {
+ // IOExceptions are normal
+ getLog().debug(sm.getString(
+ "http11protocol.proto.ioexception.debug"), e);
+ }
+ // Future developers: if you discover any other
+ // rare-but-nonfatal exceptions, catch them here, and log as
+ // above.
+ catch (Throwable e) {
+ ExceptionUtils.handleThrowable(e);
+ // any other exception or error is odd. Here we log it
+ // with "ERROR" level, so it will show up even on
+ // less-than-verbose logs.
+ getLog().error(sm.getString("http11protocol.proto.error"), e);
+ }
+ release(socket, processor, true, false);
+ return SocketState.CLOSED;
+ }
+
+ protected abstract P createProcessor();
+ protected abstract void initSsl(SocketWrapper<S> socket, P processor);
+ protected abstract void longPoll(SocketWrapper<S> socket, P processor);
+ protected abstract void release(SocketWrapper<S> socket, P processor,
+ boolean socketClosing, boolean addToPoller);
+ }
}
*
* @author Remy Maucherat
*/
-public class Http11AprProcessor extends AbstractHttp11Processor {
+public class Http11AprProcessor extends AbstractHttp11Processor<Long> {
private static final Log log = LogFactory.getLog(Http11AprProcessor.class);
*
* @throws IOException error during an I/O operation
*/
+ @Override
public SocketState process(SocketWrapper<Long> socket)
throws IOException {
RequestInfo rp = request.getRequestProcessor();
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.coyote.http11;
-import java.util.concurrent.ConcurrentHashMap;
-
import org.apache.coyote.AbstractProtocol;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.AprEndpoint;
import org.apache.tomcat.util.net.AprEndpoint.Handler;
-import org.apache.tomcat.util.net.SocketStatus;
import org.apache.tomcat.util.net.SocketWrapper;
// -------------------- Connection handler --------------------
protected static class Http11ConnectionHandler
- extends AbstractConnectionHandler implements Handler {
+ extends AbstractHttp11ConnectionHandler<Long,Http11AprProcessor> implements Handler {
protected Http11AprProtocol proto;
- protected ConcurrentHashMap<Long, Http11AprProcessor> connections =
- new ConcurrentHashMap<Long, Http11AprProcessor>();
-
- protected RecycledProcessors<Http11AprProcessor> recycledProcessors =
- new RecycledProcessors<Http11AprProcessor>(this);
-
Http11ConnectionHandler(Http11AprProtocol proto) {
this.proto = proto;
}
* @param isSocketClosing Not used in HTTP
* @param addToPoller
*/
+ @Override
public void release(SocketWrapper<Long> socket,
Http11AprProcessor processor, boolean isSocketClosing,
boolean addToPoller) {
}
@Override
- public SocketState process(SocketWrapper<Long> socket,
- SocketStatus status) {
- Http11AprProcessor processor = connections.remove(socket.getSocket());
-
- socket.setAsync(false);
-
- try {
- if (processor == null) {
- processor = recycledProcessors.poll();
- }
- if (processor == null) {
- processor = createProcessor();
- }
-
- initSsl(socket, processor);
-
- SocketState state = SocketState.CLOSED;
- do {
- if (processor.isAsync() || state == SocketState.ASYNC_END) {
- state = processor.asyncDispatch(status);
- } else if (processor.comet) {
- state = processor.event(status);
- } else {
- state = processor.process(socket);
- }
-
- if (state != SocketState.CLOSED && processor.isAsync()) {
- state = processor.asyncPostProcess();
- }
- } while (state == SocketState.ASYNC_END);
-
- if (state == SocketState.LONG) {
- // In the middle of processing a request/response. Keep the
- // socket associated with the processor. Exact requirements
- // depend on type of long poll
- longPoll(socket, processor);
- } else if (state == SocketState.OPEN){
- // In keep-alive but between requests. OK to recycle
- // processor. Continue to poll for the next request.
- release(socket, processor, false, true);
- } else {
- // Connection closed. OK to recycle the processor.
- release(socket, processor, true, false);
- }
- return state;
- } catch (java.net.SocketException e) {
- // SocketExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.socketexception.debug"), e);
- } catch (java.io.IOException e) {
- // IOExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.ioexception.debug"), e);
- }
- // Future developers: if you discover any other
- // rare-but-nonfatal exceptions, catch them here, and log as
- // above.
- catch (Throwable e) {
- ExceptionUtils.handleThrowable(e);
- // any other exception or error is odd. Here we log it
- // with "ERROR" level, so it will show up even on
- // less-than-verbose logs.
- log.error(sm.getString("http11protocol.proto.error"), e);
- }
- release(socket, processor, true, false);
- return SocketState.CLOSED;
- }
-
- @SuppressWarnings("unused")
- private void initSsl(SocketWrapper<Long> socket,
+ protected void initSsl(SocketWrapper<Long> socket,
Http11AprProcessor processor) {
// NOOP for APR
}
- private void longPoll(SocketWrapper<Long> socket,
+ @Override
+ protected void longPoll(SocketWrapper<Long> socket,
Http11AprProcessor processor) {
- connections.put(socket.getSocket(), processor);
+ connections.put(socket, processor);
if (processor.isAsync()) {
socket.setAsync(true);
}
}
+ @Override
protected Http11AprProcessor createProcessor() {
Http11AprProcessor processor = new Http11AprProcessor(
proto.getMaxHttpHeaderSize(), (AprEndpoint)proto.endpoint,
* @author Remy Maucherat
* @author Filip Hanik
*/
-public class Http11NioProcessor extends AbstractHttp11Processor {
+public class Http11NioProcessor extends AbstractHttp11Processor<NioChannel> {
private static final Log log = LogFactory.getLog(Http11NioProcessor.class);
@Override
*
* @throws IOException error during an I/O operation
*/
+ @Override
public SocketState process(SocketWrapper<NioChannel> socket)
throws IOException {
RequestInfo rp = request.getRequestProcessor();
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.coyote.http11;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.coyote.AbstractProtocol;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.ExceptionUtils;
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.NioEndpoint.KeyAttachment;
import org.apache.tomcat.util.net.SSLImplementation;
import org.apache.tomcat.util.net.SecureNioChannel;
-import org.apache.tomcat.util.net.SocketStatus;
import org.apache.tomcat.util.net.SocketWrapper;
// -------------------- Connection handler --------------------
protected static class Http11ConnectionHandler
- extends AbstractConnectionHandler implements Handler {
+ extends AbstractHttp11ConnectionHandler<NioChannel,Http11NioProcessor>
+ implements Handler {
protected Http11NioProtocol proto;
- protected ConcurrentHashMap<SocketWrapper<NioChannel>, Http11NioProcessor> connections =
- new ConcurrentHashMap<SocketWrapper<NioChannel>, Http11NioProcessor>();
-
- protected RecycledProcessors<Http11NioProcessor> recycledProcessors =
- new RecycledProcessors<Http11NioProcessor>(this);
-
Http11ConnectionHandler(Http11NioProtocol proto) {
this.proto = proto;
}
return proto.sslImplementation;
}
- @Override
- public void recycle() {
- recycledProcessors.clear();
- }
-
/**
* Expected to be used by the Poller to release resources on socket
* close, errors etc.
* @param isSocketClosing Not used in HTTP
* @param addToPoller
*/
+ @Override
public void release(SocketWrapper<NioChannel> socket,
Http11NioProcessor processor, boolean isSocketClosing,
boolean addToPoller) {
@Override
- public SocketState process(SocketWrapper<NioChannel> socket,
- SocketStatus status) {
- Http11NioProcessor processor = connections.remove(socket);
-
- socket.setAsync(false); //no longer check for timeout
-
- try {
- if (processor == null) {
- processor = recycledProcessors.poll();
- }
- if (processor == null) {
- processor = createProcessor();
- }
-
- initSsl(socket, processor);
-
- SocketState state = SocketState.CLOSED;
- do {
- if (processor.isAsync() || state == SocketState.ASYNC_END) {
- state = processor.asyncDispatch(status);
- } else if (processor.comet) {
- state = processor.event(status);
- } else {
- state = processor.process(socket);
- }
-
- if (state != SocketState.CLOSED && processor.isAsync()) {
- state = processor.asyncPostProcess();
- }
- } while (state == SocketState.ASYNC_END);
-
- if (state == SocketState.LONG) {
- // In the middle of processing a request/response. Keep the
- // socket associated with the processor. Exact requirements
- // depend on type of long poll
- longPoll(socket, processor);
- } else if (state == SocketState.OPEN){
- // In keep-alive but between requests. OK to recycle
- // processor. Continue to poll for the next request.
- release(socket, processor, false, true);
- } else {
- // Connection closed. OK to recycle the processor.
- release(socket, processor, true, false);
- }
- return state;
- } catch (java.net.SocketException e) {
- // SocketExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.socketexception.debug"), e);
- } catch (java.io.IOException e) {
- // IOExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.ioexception.debug"), e);
- }
- // Future developers: if you discover any other
- // rare-but-nonfatal exceptions, catch them here, and log as
- // above.
- catch (Throwable e) {
- ExceptionUtils.handleThrowable(e);
- // any other exception or error is odd. Here we log it
- // with "ERROR" level, so it will show up even on
- // less-than-verbose logs.
- log.error(sm.getString("http11protocol.proto.error"), e);
- }
- release(socket, processor, true, false);
- return SocketState.CLOSED;
- }
-
- private void initSsl(SocketWrapper<NioChannel> socket,
+ protected void initSsl(SocketWrapper<NioChannel> socket,
Http11NioProcessor processor) {
if (proto.isSSLEnabled() &&
(proto.sslImplementation != null)
}
- private void longPoll(SocketWrapper<NioChannel> socket,
+ @Override
+ protected void longPoll(SocketWrapper<NioChannel> socket,
Http11NioProcessor processor) {
connections.put(socket, processor);
}
}
+ @Override
public Http11NioProcessor createProcessor() {
Http11NioProcessor processor = new Http11NioProcessor(
proto.getMaxHttpHeaderSize(), (NioEndpoint)proto.endpoint,
* @author Remy Maucherat
* @author fhanik
*/
-public class Http11Processor extends AbstractHttp11Processor {
+public class Http11Processor extends AbstractHttp11Processor<Socket> {
private static final Log log = LogFactory.getLog(Http11Processor.class);
@Override
*
* @throws IOException error during an I/O operation
*/
+ @Override
public SocketState process(SocketWrapper<Socket> socketWrapper)
throws IOException {
RequestInfo rp = request.getRequestProcessor();
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.apache.coyote.http11;
import java.net.Socket;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.coyote.AbstractProtocol;
import org.apache.juli.logging.Log;
-import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.JIoEndpoint;
import org.apache.tomcat.util.net.JIoEndpoint.Handler;
import org.apache.tomcat.util.net.SSLImplementation;
-import org.apache.tomcat.util.net.SocketStatus;
import org.apache.tomcat.util.net.SocketWrapper;
// ----------------------------------- Http11ConnectionHandler Inner Class
protected static class Http11ConnectionHandler
- extends AbstractConnectionHandler implements Handler {
+ extends AbstractHttp11ConnectionHandler<Socket, Http11Processor> implements Handler {
protected Http11Protocol proto;
- protected ConcurrentHashMap<SocketWrapper<Socket>, Http11Processor> connections =
- new ConcurrentHashMap<SocketWrapper<Socket>, Http11Processor>();
-
- protected RecycledProcessors<Http11Processor> recycledProcessors =
- new RecycledProcessors<Http11Processor>(this);
-
Http11ConnectionHandler(Http11Protocol proto) {
this.proto = proto;
}
return proto.sslImplementation;
}
- @Override
- public void recycle() {
- recycledProcessors.clear();
- }
-
/**
* Expected to be used by the handler once the processor is no longer
* required.
* @param isSocketClosing Not used in HTTP
* @param addToPoller Not used in BIO
*/
+ @Override
public void release(SocketWrapper<Socket> socket,
Http11Processor processor, boolean isSocketClosing,
boolean addToPoller) {
}
@Override
- public SocketState process(SocketWrapper<Socket> socket,
- SocketStatus status) {
- Http11Processor processor = connections.remove(socket);
-
- socket.setAsync(false); //no longer check for timeout
-
- try {
- if (processor == null) {
- processor = recycledProcessors.poll();
- }
- if (processor == null) {
- processor = createProcessor();
- }
-
- initSsl(socket, processor);
-
- SocketState state = SocketState.CLOSED;
- do {
- if (processor.isAsync() || state == SocketState.ASYNC_END) {
- state = processor.asyncDispatch(status);
- } else if (processor.comet) {
- state = processor.event(status);
- } else {
- state = processor.process(socket);
- }
-
- if (state != SocketState.CLOSED && processor.isAsync()) {
- state = processor.asyncPostProcess();
- }
- } while (state == SocketState.ASYNC_END);
-
- if (state == SocketState.LONG) {
- // In the middle of processing a request/response. Keep the
- // socket associated with the processor. Exact requirements
- // depend on type of long poll
- longPoll(socket, processor);
- } else if (state == SocketState.OPEN){
- // In keep-alive but between requests. OK to recycle
- // processor. Continue to poll for the next request.
- release(socket, processor, false, true);
- } else {
- // Connection closed. OK to recycle the processor.
- release(socket, processor, true, false);
- }
- return state;
- } catch(java.net.SocketException e) {
- // SocketExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.socketexception.debug"), e);
- } catch (java.io.IOException e) {
- // IOExceptions are normal
- log.debug(sm.getString(
- "http11protocol.proto.ioexception.debug"), e);
- }
- // Future developers: if you discover any other
- // rare-but-nonfatal exceptions, catch them here, and log as
- // above.
- catch (Throwable e) {
- ExceptionUtils.handleThrowable(e);
- // any other exception or error is odd. Here we log it
- // with "ERROR" level, so it will show up even on
- // less-than-verbose logs.
- log.error(sm.getString("http11protocol.proto.error"), e);
- }
- release(socket, processor, true, false);
- return SocketState.CLOSED;
- }
-
- private void initSsl(SocketWrapper<Socket> socket,
+ protected void initSsl(SocketWrapper<Socket> socket,
Http11Processor processor) {
if (proto.isSSLEnabled() && (proto.sslImplementation != null)) {
processor.setSSLSupport(
}
- private void longPoll(SocketWrapper<Socket> socket,
+ @Override
+ protected void longPoll(SocketWrapper<Socket> socket,
Http11Processor processor) {
connections.put(socket, processor);
}
+ @Override
protected Http11Processor createProcessor() {
Http11Processor processor = new Http11Processor(
proto.getMaxHttpHeaderSize(), (JIoEndpoint)proto.endpoint,