You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2011/06/27 21:06:14 UTC
svn commit: r1140270 - in /httpcomponents/httpcore/branches/4.1.x: ./
httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/
httpcore-nio/src/main/java/org/apache/http/nio/entity/
Author: olegk
Date: Mon Jun 27 19:06:14 2011
New Revision: 1140270
URL: http://svn.apache.org/viewvc?rev=1140270&view=rev
Log:
HTTPCORE-261: IOSession#setSocketTimeout() method does not reset the timeout count (merged from trunk)
Modified:
httpcomponents/httpcore/branches/4.1.x/ (props changed)
httpcomponents/httpcore/branches/4.1.x/RELEASE_NOTES.txt
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionHandle.java
httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/SkipContentListener.java
Propchange: httpcomponents/httpcore/branches/4.1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Jun 27 19:06:14 2011
@@ -1,2 +1,2 @@
/httpcomponents/httpcore/branches/ibm_compat_branch:755687-758898
-/httpcomponents/httpcore/trunk:1102241-1102657,1133031-1134569,1134571
+/httpcomponents/httpcore/trunk:1102241-1102657,1133031-1134569,1134571,1139799-1140255
Modified: httpcomponents/httpcore/branches/4.1.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/RELEASE_NOTES.txt?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/branches/4.1.x/RELEASE_NOTES.txt Mon Jun 27 19:06:14 2011
@@ -1,6 +1,9 @@
Changes since 4.1.1
-------------------
+* [HTTPCORE-261] IOSession#setSocketTimeout() method does not reset the timeout count.
+ Contributed by Oleg Kalnichevski <olegk at apache.org>
+
* [HTTPCORE-260] Non-blocking SSL I/O session can terminate prematurely causing message body
truncation when message content is chunk coded and the connection is closed on the opposite end.
Contributed by Oleg Kalnichevski <olegk at apache.org>
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/AbstractIOReactor.java Mon Jun 27 19:06:14 2011
@@ -144,17 +144,6 @@ public abstract class AbstractIOReactor
protected abstract void writable(SelectionKey key);
/**
- * Triggered to verify whether the I/O session associated with the
- * given selection key has not timed out.
- * <p>
- * Super-classes can implement this method to react to the event.
- *
- * @param key the selection key.
- * @param now current time as long value.
- */
- protected abstract void timeoutCheck(SelectionKey key, long now);
-
- /**
* Triggered to validate keys currently registered with the selector. This
* method is called after each I/O select loop.
* <p>
@@ -174,7 +163,8 @@ public abstract class AbstractIOReactor
* @param key the selection key.
* @param session new I/O session.
*/
- protected abstract void sessionCreated(SelectionKey key, IOSession session);
+ protected void sessionCreated(final SelectionKey key, final IOSession session) {
+ }
/**
* Triggered when a session has been closed.
@@ -183,7 +173,18 @@ public abstract class AbstractIOReactor
*
* @param session closed I/O session.
*/
- protected abstract void sessionClosed(IOSession session);
+ protected void sessionClosed(final IOSession session) {
+ }
+
+ /**
+ * Triggered when a session has timed out.
+ * <p>
+ * Super-classes can implement this method to react to the event.
+ *
+ * @param session timed out I/O session.
+ */
+ protected void sessionTimedOut(final IOSession session) {
+ }
/**
* Obtains {@link IOSession} instance associated with the given selection
@@ -192,7 +193,9 @@ public abstract class AbstractIOReactor
* @param key the selection key.
* @return I/O session.
*/
- protected abstract IOSession getSession(SelectionKey key);
+ protected IOSession getSession(final SelectionKey key) {
+ return (IOSession) key.attachment();
+ }
public IOReactorStatus getStatus() {
return this.status;
@@ -324,6 +327,7 @@ public abstract class AbstractIOReactor
* @param key the selection key that triggered an event.
*/
protected void processEvent(final SelectionKey key) {
+ IOSessionImpl session = (IOSessionImpl) key.attachment();
try {
if (key.isAcceptable()) {
acceptable(key);
@@ -332,13 +336,14 @@ public abstract class AbstractIOReactor
connectable(key);
}
if (key.isReadable()) {
+ session.resetLastRead();
readable(key);
}
if (key.isWritable()) {
+ session.resetLastWrite();
writable(key);
}
} catch (CancelledKeyException ex) {
- IOSession session = getSession(key);
queueClosedSession(session);
key.attach(null);
}
@@ -416,6 +421,7 @@ public abstract class AbstractIOReactor
if (sessionRequest != null) {
sessionRequest.completed(session);
}
+ key.attach(session);
sessionCreated(key, session);
} catch (CancelledKeyException ex) {
queueClosedSession(session);
@@ -469,6 +475,27 @@ public abstract class AbstractIOReactor
}
/**
+ * Triggered to verify whether the I/O session associated with the
+ * given selection key has not timed out.
+ * <p>
+ * Super-classes can implement this method to react to the event.
+ *
+ * @param key the selection key.
+ * @param now current time as long value.
+ */
+ protected void timeoutCheck(final SelectionKey key, long now) {
+ IOSessionImpl session = (IOSessionImpl) key.attachment();
+ if (session != null) {
+ int timeout = session.getSocketTimeout();
+ if (timeout > 0) {
+ if (session.getLastAccessTime() + timeout < now) {
+ sessionTimedOut(session);
+ }
+ }
+ }
+ }
+
+ /**
* Closes out all I/O sessions maintained by this I/O reactor.
*/
protected void closeSessions() {
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/BaseIOReactor.java Mon Jun 27 19:06:14 2011
@@ -109,7 +109,7 @@ public class BaseIOReactor extends Abstr
*
* @param exceptionHandler the exception handler.
*/
- public void setExceptionHandler(IOReactorExceptionHandler exceptionHandler) {
+ public void setExceptionHandler(final IOReactorExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
@@ -153,10 +153,7 @@ public class BaseIOReactor extends Abstr
*/
@Override
protected void readable(final SelectionKey key) {
- SessionHandle handle = (SessionHandle) key.attachment();
- IOSession session = handle.getSession();
- handle.resetLastRead();
-
+ IOSession session = getSession(key);
try {
this.eventDispatch.inputReady(session);
if (session.hasBufferedInput()) {
@@ -177,10 +174,7 @@ public class BaseIOReactor extends Abstr
*/
@Override
protected void writable(final SelectionKey key) {
- SessionHandle handle = (SessionHandle) key.attachment();
- IOSession session = handle.getSession();
- handle.resetLastWrite();
-
+ IOSession session = getSession(key);
try {
this.eventDispatch.outputReady(session);
} catch (CancelledKeyException ex) {
@@ -247,57 +241,32 @@ public class BaseIOReactor extends Abstr
}
/**
- * Performs timeout check for the I/O session associated with the given
- * selection key.
- */
- @Override
- protected void timeoutCheck(final SelectionKey key, long now) {
- Object attachment = key.attachment();
- if (attachment instanceof SessionHandle) {
- SessionHandle handle = (SessionHandle) key.attachment();
- IOSession session = handle.getSession();
- int timeout = session.getSocketTimeout();
- if (timeout > 0) {
- if (handle.getLastAccessTime() + timeout < now) {
- try {
- this.eventDispatch.timeout(session);
- } catch (CancelledKeyException ex) {
- queueClosedSession(session);
- key.attach(null);
- } catch (RuntimeException ex) {
- handleRuntimeException(ex);
- }
- }
- }
- }
- }
-
- /**
* Processes newly created I/O session. This method dispatches the event
* notification to the {@link IOEventDispatch#connected(IOSession)} method.
*/
@Override
protected void sessionCreated(final SelectionKey key, final IOSession session) {
- SessionHandle handle = new SessionHandle(session);
- key.attach(handle);
try {
this.eventDispatch.connected(session);
} catch (CancelledKeyException ex) {
queueClosedSession(session);
- key.attach(null);
} catch (RuntimeException ex) {
handleRuntimeException(ex);
}
}
+ /**
+ * Processes timed out I/O session. This method dispatches the event
+ * notification to the {@link IOEventDispatch#timeout(IOSession)} method.
+ */
@Override
- protected IOSession getSession(final SelectionKey key) {
- Object attachment = key.attachment();
- if (attachment instanceof SessionHandle) {
- SessionHandle handle = (SessionHandle) attachment;
- return handle.getSession();
- } else {
- return null;
+ protected void sessionTimedOut(final IOSession session) {
+ try {
+ this.eventDispatch.timeout(session);
+ } catch (CancelledKeyException ex) {
+ queueClosedSession(session);
+ } catch (RuntimeException ex) {
+ handleRuntimeException(ex);
}
}
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/IOSessionImpl.java Mon Jun 27 19:06:14 2011
@@ -58,6 +58,12 @@ public class IOSessionImpl implements IO
private volatile SessionBufferStatus bufferStatus;
private volatile int socketTimeout;
+ private final long startedTime;
+
+ private long lastReadTime;
+ private long lastWriteTime;
+ private long lastAccessTime;
+
/**
* Creates new instance of IOSessionImpl.
*
@@ -83,6 +89,11 @@ public class IOSessionImpl implements IO
this.currentEventMask = 0;
this.socketTimeout = 0;
this.status = ACTIVE;
+ long now = System.currentTimeMillis();
+ this.startedTime = now;
+ this.lastReadTime = now;
+ this.lastWriteTime = now;
+ this.lastAccessTime = now;
}
/**
@@ -186,8 +197,9 @@ public class IOSessionImpl implements IO
return this.socketTimeout;
}
- public void setSocketTimeout(int timeout) {
+ public synchronized void setSocketTimeout(int timeout) {
this.socketTimeout = timeout;
+ this.lastAccessTime = System.currentTimeMillis();
}
public synchronized void close() {
@@ -250,6 +262,34 @@ public class IOSessionImpl implements IO
this.attributes.put(name, obj);
}
+ synchronized long getStartedTime() {
+ return this.startedTime;
+ }
+
+ synchronized long getLastReadTime() {
+ return this.lastReadTime;
+ }
+
+ synchronized long getLastWriteTime() {
+ return this.lastWriteTime;
+ }
+
+ synchronized long getLastAccessTime() {
+ return this.lastAccessTime;
+ }
+
+ synchronized void resetLastRead() {
+ long now = System.currentTimeMillis();
+ this.lastReadTime = now;
+ this.lastAccessTime = now;
+ }
+
+ synchronized void resetLastWrite() {
+ long now = System.currentTimeMillis();
+ this.lastWriteTime = now;
+ this.lastAccessTime = now;
+ }
+
private static void formatOps(final StringBuffer buffer, int ops) {
if ((ops & SelectionKey.OP_READ) > 0) {
buffer.append('r');
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SSLIOSession.java Mon Jun 27 19:06:14 2011
@@ -235,6 +235,9 @@ public class SSLIOSession implements IOS
}
private int receiveEncryptedData() throws IOException {
+ if (this.endOfStream) {
+ return -1;
+ }
return this.session.channel().read(this.inEncrypted);
}
@@ -456,7 +459,7 @@ public class SSLIOSession implements IOS
buffer.append("][");
buffer.append(this.sslEngine.getHandshakeStatus());
if (this.endOfStream) {
- buffer.append("EOF][");
+ buffer.append("][EOF][");
}
buffer.append("][");
buffer.append(this.inEncrypted.position());
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionHandle.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionHandle.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionHandle.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionHandle.java Mon Jun 27 19:06:14 2011
@@ -34,8 +34,11 @@ import org.apache.http.nio.reactor.IOSes
* to a {@link IOSession} along with information about time of last I/O
* operations on that session.
*
+ * @deprecated use {@link IOSessionImpl}
+ *
* @since 4.0
*/
+@Deprecated
public class SessionHandle {
private final IOSession session;
Modified: httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/SkipContentListener.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/SkipContentListener.java?rev=1140270&r1=1140269&r2=1140270&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/SkipContentListener.java (original)
+++ httpcomponents/httpcore/branches/4.1.x/httpcore-nio/src/main/java/org/apache/http/nio/entity/SkipContentListener.java Mon Jun 27 19:06:14 2011
@@ -54,13 +54,10 @@ public class SkipContentListener impleme
public void contentAvailable(
final ContentDecoder decoder,
final IOControl ioctrl) throws IOException {
- int totalRead = 0;
int lastRead;
do {
buffer.clear();
lastRead = decoder.read(buffer);
- if (lastRead > 0)
- totalRead += lastRead;
} while (lastRead > 0);
}