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/08 18:43:07 UTC

svn commit: r1358778 - 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: Sun Jul  8 16:43:07 2012
New Revision: 1358778

URL: http://svn.apache.org/viewvc?rev=1358778&view=rev
Log:
Do not suspend input notifications even if no input is expected

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestPipelining.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java?rev=1358778&r1=1358777&r2=1358778&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java Sun Jul  8 16:43:07 2012
@@ -271,7 +271,6 @@ public class HttpAsyncService implements
         } else {
             // No request content is expected.
             // Process request right away
-            conn.suspendInput();
             processRequest(conn, state);
         }
     }
@@ -284,7 +283,6 @@ public class HttpAsyncService implements
         consumer.consumeContent(decoder, conn);
         state.setRequestState(MessageState.BODY_STREAM);
         if (decoder.isCompleted()) {
-            conn.suspendInput();
             processRequest(conn, state);
         }
     }
@@ -761,6 +759,11 @@ public class HttpAsyncService implements
                     this.state.setResponseProducer(responseProducer);
                     this.state.setCancellable(null);
                     this.conn.requestOutput();
+                } else {
+                    try {
+                        responseProducer.close();
+                    } catch (IOException ex) {
+                    }
                 }
             }
         }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java?rev=1358778&r1=1358777&r2=1358778&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncHandlerCancellable.java Sun Jul  8 16:43:07 2012
@@ -39,19 +39,27 @@ import java.util.concurrent.TimeUnit;
 import org.apache.http.HttpCoreNIOTestBase;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
 import org.apache.http.LoggingClientConnectionFactory;
 import org.apache.http.LoggingServerConnectionFactory;
 import org.apache.http.concurrent.Cancellable;
+import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.entity.ContentType;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
 import org.apache.http.impl.DefaultHttpResponseFactory;
 import org.apache.http.impl.nio.DefaultNHttpClientConnection;
 import org.apache.http.impl.nio.DefaultNHttpServerConnection;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
 import org.apache.http.nio.NHttpConnectionFactory;
 import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
 import org.apache.http.nio.protocol.HttpAsyncExchange;
 import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
 import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
 import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
+import org.apache.http.nio.protocol.HttpAsyncResponseProducer;
 import org.apache.http.nio.protocol.HttpAsyncService;
 import org.apache.http.nio.reactor.IOReactorStatus;
 import org.apache.http.nio.reactor.ListenerEndpoint;
@@ -87,6 +95,90 @@ public class TestHttpAsyncHandlerCancell
     }
 
     @Test
+    public void testResponsePrematureTermination() throws Exception {
+        
+        final CountDownLatch latch = new CountDownLatch(1);
+        final HttpAsyncResponseProducer responseProducer = new HttpAsyncResponseProducer() {
+            
+            public HttpResponse generateResponse() {
+                HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+                BasicHttpEntity entity = new BasicHttpEntity();
+                entity.setContentType(ContentType.DEFAULT_BINARY.toString());
+                entity.setChunked(true);
+                response.setEntity(entity);
+                return response;
+            }
+            
+            public void close() throws IOException {
+                latch.countDown();
+            }
+            
+            public void responseCompleted(final HttpContext context) {
+            }
+            
+            public void produceContent(
+                    final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
+                // suspend output
+                ioctrl.suspendOutput();
+            }
+            
+            public void failed(final Exception ex) {
+            }
+
+        };
+        
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
+
+            public HttpAsyncRequestConsumer<HttpRequest> processRequest(
+                    final HttpRequest request,
+                    final HttpContext context) throws HttpException, IOException {
+                return new BasicAsyncRequestConsumer();
+            }
+
+            public void handle(
+                    final HttpRequest data, 
+                    final HttpAsyncExchange httpExchange, 
+                    final HttpContext context)
+                    throws HttpException, IOException {
+                httpExchange.submitResponse(responseProducer);
+            }
+            
+        });
+        HttpAsyncService serviceHandler = new HttpAsyncService(
+                this.serverHttpProc,
+                new DefaultConnectionReuseStrategy(),
+                new DefaultHttpResponseFactory(),
+                registry,
+                null,
+                this.serverParams);
+        this.server.start(serviceHandler);
+
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+
+        Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
+        InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
+        Socket socket = new Socket("localhost", address.getPort());
+        try {
+            OutputStream outstream = socket.getOutputStream();
+            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outstream, "US-ASCII"));
+            writer.write("GET /long HTTP/1.1\r\n");
+            writer.write("Host: localhost\r\n");
+            writer.write("\r\n");
+            writer.flush();
+            
+            Thread.sleep(250);
+            
+            writer.close();
+        } finally {
+            socket.close();
+        }
+
+        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+    }
+
+    @Test
     public void testRequestCancelled() throws Exception {
         
         final CountDownLatch latch = new CountDownLatch(1);

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestPipelining.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestPipelining.java?rev=1358778&r1=1358777&r2=1358778&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestPipelining.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestPipelining.java Sun Jul  8 16:43:07 2012
@@ -156,20 +156,28 @@ public class TestPipelining extends Http
             }
             reader.close();
             writer.close();
+//            String expected =
+//                    "HTTP/1.1 200 OK\r\n" +
+//                    "Server: TEST-SERVER/1.1\r\n" +
+//                    "Content-Length: 19\r\n" +
+//                    "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
+//                    "\r\n" +
+//                    "thank you very much" +
+//                    "HTTP/1.1 200 OK\r\n" +
+//                    "Server: TEST-SERVER/1.1\r\n" +
+//                    "Content-Length: 19\r\n" +
+//                    "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
+//                    "Connection: close\r\n" +
+//                    "\r\n" +
+//                    "thank you very much";
             String expected =
-                    "HTTP/1.1 200 OK\r\n" +
-                    "Server: TEST-SERVER/1.1\r\n" +
-                    "Content-Length: 19\r\n" +
-                    "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
-                    "\r\n" +
-                    "thank you very much" +
-                    "HTTP/1.1 200 OK\r\n" +
+                    "HTTP/1.1 400 Bad Request\r\n" +
+                    "Connection: Close\r\n" +
                     "Server: TEST-SERVER/1.1\r\n" +
-                    "Content-Length: 19\r\n" +
+                    "Content-Length: 70\r\n" +
                     "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
-                    "Connection: close\r\n" +
                     "\r\n" +
-                    "thank you very much";
+                    "Out of sequence request message detected (pipelining is not supported)";
             Assert.assertEquals(expected, buf.toString());
 
         } finally {

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java?rev=1358778&r1=1358777&r2=1358778&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java Sun Jul  8 16:43:07 2012
@@ -310,7 +310,6 @@ public class TestHttpAsyncService {
 
         Mockito.verify(this.httpProcessor).process(request, exchangeContext);
         Mockito.verify(this.requestConsumer).requestReceived(request);
-        Mockito.verify(this.conn).suspendInput();
         Mockito.verify(this.requestConsumer).requestCompleted(exchangeContext);
         Mockito.verify(this.requestHandler).handle(
                 Mockito.eq(data),
@@ -555,7 +554,6 @@ public class TestHttpAsyncService {
         Assert.assertEquals(MessageState.INIT, state.getResponseState());
 
         Mockito.verify(this.requestConsumer).consumeContent(this.decoder, this.conn);
-        Mockito.verify(this.conn).suspendInput();
         Mockito.verify(this.requestConsumer).requestCompleted(exchangeContext);
         Mockito.verify(this.requestHandler).handle(
                 Mockito.eq(data),
@@ -585,7 +583,6 @@ public class TestHttpAsyncService {
         Assert.assertNotNull(state.getResponseProducer());
 
         Mockito.verify(this.requestConsumer).consumeContent(this.decoder, this.conn);
-        Mockito.verify(this.conn).suspendInput();
         Mockito.verify(this.requestConsumer).requestCompleted(exchangeContext);
         Mockito.verify(this.conn).requestOutput();
         Mockito.verify(this.requestHandler, Mockito.never()).handle(
@@ -622,7 +619,6 @@ public class TestHttpAsyncService {
         Assert.assertNotNull(state.getResponseProducer());
 
         Mockito.verify(this.requestConsumer).consumeContent(this.decoder, this.conn);
-        Mockito.verify(this.conn).suspendInput();
         Mockito.verify(this.requestConsumer).requestCompleted(exchangeContext);
         Mockito.verify(this.conn).requestOutput();
     }