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 2012/07/21 15:42:50 UTC

svn commit: r1364092 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/integration/ test/java/org/apache/http/nio/protocol/

Author: olegk
Date: Sat Jul 21 13:42:50 2012
New Revision: 1364092

URL: http://svn.apache.org/viewvc?rev=1364092&view=rev
Log:
HTTPASYNC-21: request execution handler does not get closed in case of a premature HTTP exchange termination

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java?rev=1364092&r1=1364091&r2=1364092&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java Sat Jul 21 13:42:50 2012
@@ -31,6 +31,7 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.net.SocketTimeoutException;
 
+import org.apache.http.ConnectionClosedException;
 import org.apache.http.ConnectionReuseStrategy;
 import org.apache.http.HttpConnection;
 import org.apache.http.HttpEntityEnclosingRequest;
@@ -103,10 +104,11 @@ public class HttpAsyncRequestExecutor im
     public void closed(final NHttpClientConnection conn) {
         State state = getState(conn);
         HttpAsyncRequestExecutionHandler<?> handler = getHandler(conn);
+        if (state == null || !state.isValid()) {
+            closeHandler(handler, null);
+        }
         if (state != null) {
             state.reset();
-        } else {
-            closeHandler(handler, null);
         }
     }
 
@@ -251,6 +253,13 @@ public class HttpAsyncRequestExecutor im
     }
 
     public void endOfInput(final NHttpClientConnection conn) throws IOException {
+        State state = getState(conn);
+        if (state != null) {
+            if (state.getRequestState().compareTo(MessageState.READY) != 0) {
+                state.invalidate();
+                closeHandler(getHandler(conn), new ConnectionClosedException("Connection closed"));
+            }
+        }
         conn.close();
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java?rev=1364092&r1=1364091&r2=1364092&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java Sat Jul 21 13:42:50 2012
@@ -71,7 +71,6 @@ import org.apache.http.protocol.HttpCont
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class TestHttpAsyncPrematureTermination extends HttpCoreNIOTestBase {
@@ -122,7 +121,7 @@ public class TestHttpAsyncPrematureTermi
         return (InetSocketAddress) endpoint.getAddress();
     }
 
-    @Test @Ignore
+    @Test
     public void testConnectionTerminatedProcessingRequest() throws Exception {
         HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
         registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
@@ -177,7 +176,7 @@ public class TestHttpAsyncPrematureTermi
         Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
     }
 
-    @Test @Ignore
+    @Test
     public void testConnectionTerminatedHandlingRequest() throws Exception {
         HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
         registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java?rev=1364092&r1=1364091&r2=1364092&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java Sat Jul 21 13:42:50 2012
@@ -32,6 +32,7 @@ import java.net.SocketTimeoutException;
 
 import junit.framework.Assert;
 
+import org.apache.http.ConnectionClosedException;
 import org.apache.http.ConnectionReuseStrategy;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
@@ -533,6 +534,31 @@ public class TestHttpAsyncRequestExecuto
     }
 
     @Test
+    public void testEndOfInput() throws Exception {
+        State state = new HttpAsyncRequestExecutor.State();
+        this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);
+
+        this.protocolHandler.endOfInput(this.conn);
+
+        Mockito.verify(this.conn).close();
+    }
+
+    @Test
+    public void testPrematureEndOfInput() throws Exception {
+        State state = new HttpAsyncRequestExecutor.State();
+        state.setRequestState(MessageState.COMPLETED);
+        this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);
+        this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler);
+
+        this.protocolHandler.endOfInput(this.conn);
+
+        Assert.assertFalse(state.isValid());
+
+        Mockito.verify(this.conn).close();
+        Mockito.verify(this.exchangeHandler).failed(Mockito.any(ConnectionClosedException.class));
+    }
+
+    @Test
     public void testTimeoutNoHandler() throws Exception {
         State state = new HttpAsyncRequestExecutor.State();
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);