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 2008/03/01 14:13:54 UTC

svn commit: r632589 - in /httpcomponents/httpcore/trunk/module-nio/src: main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java

Author: olegk
Date: Sat Mar  1 05:13:53 2008
New Revision: 632589

URL: http://svn.apache.org/viewvc?rev=632589&view=rev
Log:
HTTPCORE-152: Fixed handling of responses without an entity body
Contributed by Sam Berlin <sberlin at gmail.com> 

Modified:
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java
    httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java?rev=632589&r1=632588&r2=632589&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/AsyncNHttpServiceHandler.java Sat Mar  1 05:13:53 2008
@@ -358,6 +358,7 @@
                     connState.reset();
                     conn.requestInput();
                 }
+                responseComplete(response, context);
             }
 
         } catch (IOException ex) {
@@ -451,7 +452,11 @@
         if (entity != null) {
             ProducingNHttpEntity producingEntity = (ProducingNHttpEntity) entity;
             connState.setProducingEntity(producingEntity);
-        } else {
+        }
+
+        conn.submitResponse(response);
+
+        if (entity == null) {
             if (!this.connStrategy.keepAlive(response, context)) {
                 conn.close();
             } else {
@@ -459,9 +464,17 @@
                 connState.reset();
                 conn.requestInput();
             }
+            responseComplete(response, context);
         }
+    }
 
-        conn.submitResponse(response);
+    /**
+     * Signals that this response has been fully sent. This will be called after
+     * submitting the response to a connection, if there is no entity in the
+     * response. If there is an entity, it will be called after the entity has
+     * completed.
+     */
+    protected void responseComplete(HttpResponse response, HttpContext context) {
     }
 
     private NHttpRequestHandler getRequestHandler(HttpRequest request) {

Modified: httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java?rev=632589&r1=632588&r2=632589&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java (original)
+++ httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java Sat Mar  1 05:13:53 2008
@@ -1277,7 +1277,6 @@
      * {@link NHttpResponseTrigger} works correctly.
      */
     public void testDelayedHttpGets() throws Exception {
-
         final int connNo = 3;
         final int reqNo = 20;
         final RequestCount requestCount = new RequestCount(connNo * reqNo);
@@ -1647,4 +1646,87 @@
     }
     
 
+    /**
+     * This test makes sure that if no service handler is installed, things still work.
+     */
+    public void testNoServiceHandler() throws Exception {
+
+        final int connNo = 3;
+        final int reqNo = 1;
+        final RequestCount requestCount = new RequestCount(connNo * reqNo);
+
+        NHttpRequestExecutionHandler requestExecutionHandler = new NHttpRequestExecutionHandler() {
+
+            public void initalizeContext(final HttpContext context, final Object attachment) {
+                context.setAttribute("REQ-COUNT", new Integer(0));
+                context.setAttribute("RES-COUNT", new Integer(0));
+            }
+
+            public void finalizeContext(final HttpContext context) {
+            }
+
+            public HttpRequest submitRequest(final HttpContext context) {
+                int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
+                BasicHttpRequest get = null;
+                if (i < reqNo) {
+                    get = new BasicHttpRequest("GET", "/?" + i);
+                    context.setAttribute("REQ-COUNT", new Integer(i + 1));
+                }
+                return get;
+            }
+
+            public ConsumingNHttpEntity responseEntity(
+                    final HttpResponse response,
+                    final HttpContext context) throws IOException {
+                return new BufferingNHttpEntity(response.getEntity(),
+                        new HeapByteBufferAllocator());
+            }
+
+            public void handleResponse(final HttpResponse response, final HttpContext context) {
+                NHttpConnection conn = (NHttpConnection) context.getAttribute(
+                        ExecutionContext.HTTP_CONNECTION);
+                
+                int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
+                i++;
+                context.setAttribute("RES-COUNT", new Integer(i));
+
+                if(response.getStatusLine().getStatusCode() == 501)
+                    requestCount.decrement();
+                else
+                    requestCount.abort();
+
+                if (i < reqNo) {
+                    conn.requestInput();
+                }
+            }
+
+        };
+
+        NHttpServiceHandler serviceHandler = createHttpServiceHandler(
+                null,
+                null);
+
+        NHttpClientHandler clientHandler = createHttpClientHandler(
+                requestExecutionHandler);
+
+        this.server.start(serviceHandler);
+        this.client.start(clientHandler);
+
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+        InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
+
+        for (int i = 0; i < connNo; i++) {
+            this.client.openConnection(
+                    new InetSocketAddress("localhost", serverAddress.getPort()),
+                    null);
+        }
+
+        requestCount.await(10000);
+        assertEquals(0, requestCount.getValue());
+
+        this.client.shutdown();
+        this.server.shutdown();
+
+    }
 }