hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1364091 - in /httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/integration/ test/java/org/apache/http/nio/protocol/
Date Sat, 21 Jul 2012 13:41:00 GMT
Author: olegk
Date: Sat Jul 21 13:40:59 2012
New Revision: 1364091

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

Added:
    httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
  (with props)
Modified:
    httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
    httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java

Modified: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java?rev=1364091&r1=1364090&r2=1364091&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
(original)
+++ httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
Sat Jul 21 13:40:59 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();
     }
 

Added: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java?rev=1364091&view=auto
==============================================================================
--- httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
(added)
+++ httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
Sat Jul 21 13:40:59 2012
@@ -0,0 +1,295 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.nio.integration;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.http.HttpConnection;
+import org.apache.http.HttpCoreNIOTestBase;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.LoggingClientConnectionFactory;
+import org.apache.http.LoggingServerConnectionFactory;
+import org.apache.http.concurrent.FutureCallback;
+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.BasicHttpRequest;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.NHttpConnectionFactory;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
+import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
+import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
+import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
+import org.apache.http.nio.protocol.HttpAsyncExchange;
+import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
+import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
+import org.apache.http.nio.protocol.HttpAsyncRequestExecutor;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerRegistry;
+import org.apache.http.nio.protocol.HttpAsyncRequestHandlerResolver;
+import org.apache.http.nio.protocol.HttpAsyncService;
+import org.apache.http.nio.reactor.IOReactorStatus;
+import org.apache.http.nio.reactor.ListenerEndpoint;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestHttpAsyncPrematureTermination extends HttpCoreNIOTestBase {
+
+    @Before
+    public void setUp() throws Exception {
+        initServer();
+        initClient();
+        initConnPool();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        shutDownClient();
+        shutDownServer();
+    }
+
+    @Override
+    protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
+            final HttpParams params) throws Exception {
+        return new LoggingServerConnectionFactory(params);
+    }
+
+    @Override
+    protected NHttpConnectionFactory<DefaultNHttpClientConnection> createClientConnectionFactory(
+            final HttpParams params) throws Exception {
+        return new LoggingClientConnectionFactory(params);
+    }
+
+    private InetSocketAddress start(
+            final HttpAsyncRequestHandlerResolver requestHandlerResolver,
+            final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
+        HttpAsyncService serviceHandler = new HttpAsyncService(
+                this.serverHttpProc,
+                new DefaultConnectionReuseStrategy(),
+                new DefaultHttpResponseFactory(),
+                requestHandlerResolver,
+                expectationVerifier,
+                this.serverParams);
+        HttpAsyncRequestExecutor clientHandler = new HttpAsyncRequestExecutor();
+        this.server.start(serviceHandler);
+        this.client.start(clientHandler);
+
+        ListenerEndpoint endpoint = this.server.getListenerEndpoint();
+        endpoint.waitFor();
+
+        Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
+        return (InetSocketAddress) endpoint.getAddress();
+    }
+
+    @Test
+    public void testConnectionTerminatedProcessingRequest() throws Exception {
+        HttpAsyncRequestHandlerRegistry registry = new HttpAsyncRequestHandlerRegistry();
+        registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
+
+            public HttpAsyncRequestConsumer<HttpRequest> processRequest(
+                    final HttpRequest request,
+                    final HttpContext context) throws HttpException, IOException {
+                HttpConnection conn = (HttpConnection) context.getAttribute(
+                        ExecutionContext.HTTP_CONNECTION);
+                conn.shutdown();
+                return new BasicAsyncRequestConsumer();
+            }
+
+            public void handle(
+                    final HttpRequest request,
+                    final HttpAsyncExchange httpExchange,
+                    final HttpContext context) throws HttpException, IOException {
+                HttpResponse response = httpExchange.getResponse();
+                response.setEntity(new NStringEntity("all is well", ContentType.TEXT_PLAIN));
+                httpExchange.submitResponse();
+            }
+
+        });
+        InetSocketAddress address = start(registry, null);
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        final CountDownLatch latch = new CountDownLatch(1);
+
+        FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>()
{
+
+            public void cancelled() {
+                latch.countDown();
+            }
+
+            public void failed(final Exception ex) {
+                latch.countDown();
+            }
+
+            public void completed(final HttpResponse response) {
+                Assert.fail();
+            }
+
+        };
+
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        HttpContext context = new BasicHttpContext();
+        this.executor.execute(
+                new BasicAsyncRequestProducer(target, request),
+                new BasicAsyncResponseConsumer(),
+                this.connpool, context, callback);
+
+        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+    }
+
+    @Test
+    public void testConnectionTerminatedHandlingRequest() throws Exception {
+        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 request,
+                    final HttpAsyncExchange httpExchange,
+                    final HttpContext context) throws HttpException, IOException {
+                HttpConnection conn = (HttpConnection) context.getAttribute(
+                        ExecutionContext.HTTP_CONNECTION);
+                conn.shutdown();
+                HttpResponse response = httpExchange.getResponse();
+                response.setEntity(new NStringEntity("all is well", ContentType.TEXT_PLAIN));
+                httpExchange.submitResponse();
+            }
+
+        });
+        InetSocketAddress address = start(registry, null);
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        final CountDownLatch latch = new CountDownLatch(1);
+
+        FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>()
{
+
+            public void cancelled() {
+                latch.countDown();
+            }
+
+            public void failed(final Exception ex) {
+                latch.countDown();
+            }
+
+            public void completed(final HttpResponse response) {
+                Assert.fail();
+            }
+
+        };
+
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        HttpContext context = new BasicHttpContext();
+        this.executor.execute(
+                new BasicAsyncRequestProducer(target, request),
+                new BasicAsyncResponseConsumer(),
+                this.connpool, context, callback);
+
+        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+    }
+
+    @Test
+    public void testConnectionTerminatedSendingResponse() throws Exception {
+        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 request,
+                    final HttpAsyncExchange httpExchange,
+                    final HttpContext context) throws HttpException, IOException {
+                HttpResponse response = httpExchange.getResponse();
+                response.setEntity(new NStringEntity("all is well", ContentType.TEXT_PLAIN));
+                httpExchange.submitResponse(new BasicAsyncResponseProducer(response) {
+
+                    @Override
+                    public synchronized void produceContent(
+                            final ContentEncoder encoder,
+                            final IOControl ioctrl) throws IOException {
+                        ioctrl.shutdown();
+                    }
+
+                });
+            }
+
+        });
+        InetSocketAddress address = start(registry, null);
+        HttpHost target = new HttpHost("localhost", address.getPort());
+
+        final CountDownLatch latch = new CountDownLatch(1);
+
+        FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>()
{
+
+            public void cancelled() {
+                latch.countDown();
+            }
+
+            public void failed(final Exception ex) {
+                latch.countDown();
+            }
+
+            public void completed(final HttpResponse response) {
+                Assert.fail();
+            }
+
+        };
+
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        HttpContext context = new BasicHttpContext();
+        this.executor.execute(
+                new BasicAsyncRequestProducer(target, request),
+                new BasicAsyncResponseConsumer(),
+                this.connpool, context, callback);
+
+        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
+    }
+
+}

Propchange: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/integration/TestHttpAsyncPrematureTermination.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java?rev=1364091&r1=1364090&r2=1364091&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
(original)
+++ httpcomponents/httpcore/branches/4.2.x/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
Sat Jul 21 13:40:59 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);



Mime
View raw message