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 2019/10/18 09:35:32 UTC

[httpcomponents-core] branch 4.4.x updated (ae609d6 -> e7b4f5c)

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a change to branch 4.4.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git.


    from ae609d6  Conscrypt JSSE provider integration tests
     new f75d0de  HTTPCORE-607: HttpAsyncService incorrectly attempts to submit a response over idle connection in case of a timeout (no prior request); cleaned up exception handling logic in HttpAsyncService; reverted HTTPCORE-482
     new e7b4f5c  Better fix for HTTPCORE-482; code cleanup in NHttpReverseProxy

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../http/examples/nio/NHttpReverseProxy.java       |  61 ++++++------
 .../impl/nio/DefaultNHttpServerConnection.java     |   1 -
 .../apache/http/nio/protocol/HttpAsyncService.java | 110 ++++++++++++---------
 .../http/nio/protocol/TestHttpAsyncService.java    |  19 +++-
 4 files changed, 112 insertions(+), 79 deletions(-)


[httpcomponents-core] 02/02: Better fix for HTTPCORE-482; code cleanup in NHttpReverseProxy

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch 4.4.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit e7b4f5cc306b3eb4b6e1e24627971049001ddd68
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Oct 17 16:35:09 2019 +0200

    Better fix for HTTPCORE-482; code cleanup in NHttpReverseProxy
---
 .../http/examples/nio/NHttpReverseProxy.java       | 61 +++++++++++-----------
 1 file changed, 30 insertions(+), 31 deletions(-)

diff --git a/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpReverseProxy.java b/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpReverseProxy.java
index 6d97484..d830044 100644
--- a/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpReverseProxy.java
+++ b/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpReverseProxy.java
@@ -29,6 +29,7 @@ package org.apache.http.examples.nio;
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.net.InetSocketAddress;
+import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.security.cert.CertificateException;
@@ -43,9 +44,7 @@ import org.apache.http.HttpEntityEnclosingRequest;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
 import org.apache.http.config.ConnectionConfig;
@@ -71,9 +70,10 @@ import org.apache.http.nio.IOControl;
 import org.apache.http.nio.NHttpClientConnection;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServerConnection;
+import org.apache.http.nio.NHttpServerEventHandler;
 import org.apache.http.nio.entity.NStringEntity;
 import org.apache.http.nio.pool.NIOConnFactory;
-import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
+import org.apache.http.nio.protocol.ErrorResponseProducer;
 import org.apache.http.nio.protocol.HttpAsyncExchange;
 import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
 import org.apache.http.nio.protocol.HttpAsyncRequestExecutor;
@@ -143,32 +143,30 @@ public class NHttpReverseProxy {
 
         System.out.println("Reverse proxy to " + targetHost);
 
-        final IOReactorConfig config = IOReactorConfig.custom()
-            .setIoThreadCount(1)
-            .setSoTimeout(3000)
-            .setConnectTimeout(3000)
-            .build();
-        final ConnectingIOReactor connectingIOReactor = new DefaultConnectingIOReactor(config);
-        final ListeningIOReactor listeningIOReactor = new DefaultListeningIOReactor(config);
+        final ConnectingIOReactor connectingIOReactor = new DefaultConnectingIOReactor(IOReactorConfig.custom()
+                .setIoThreadCount(1)
+                .setSoTimeout(5000)
+                .setConnectTimeout(5000)
+                .build());
+        final ListeningIOReactor listeningIOReactor = new DefaultListeningIOReactor(IOReactorConfig.custom()
+                .setIoThreadCount(1)
+                .setSoTimeout(30000)
+                .build());
 
         // Set up HTTP protocol processor for incoming connections
         final HttpProcessor inhttpproc = new ImmutableHttpProcessor(
-                new HttpResponseInterceptor[] {
-                        new ResponseDate(),
-                        new ResponseServer("Test/1.1"),
-                        new ResponseContent(),
-                        new ResponseConnControl()
-         });
+                new ResponseDate(),
+                new ResponseServer("Test/1.1"),
+                new ResponseContent(),
+                new ResponseConnControl());
 
         // Set up HTTP protocol processor for outgoing connections
         final HttpProcessor outhttpproc = new ImmutableHttpProcessor(
-                new HttpRequestInterceptor[] {
-                        new RequestContent(),
-                        new RequestTargetHost(),
-                        new RequestConnControl(),
-                        new RequestUserAgent("Test/1.1"),
-                        new RequestExpectContinue(true)
-        });
+                new RequestContent(),
+                new RequestTargetHost(),
+                new RequestConnControl(),
+                new RequestUserAgent("Test/1.1"),
+                new RequestExpectContinue(true));
 
         final ProxyClientProtocolHandler clientHandler = new ProxyClientProtocolHandler();
         final HttpAsyncRequester executor = new HttpAsyncRequester(
@@ -193,7 +191,7 @@ public class NHttpReverseProxy {
         final IOEventDispatch connectingEventDispatch = DefaultHttpClientIODispatch.create(
                 clientHandler, sslContext, ConnectionConfig.DEFAULT);
 
-        final IOEventDispatch listeningEventDispatch = new DefaultHttpServerIODispatch(
+        final IOEventDispatch listeningEventDispatch = new DefaultHttpServerIODispatch<NHttpServerEventHandler>(
                 serviceHandler, ConnectionConfig.DEFAULT);
 
         final Thread t = new Thread(new Runnable() {
@@ -403,14 +401,14 @@ public class NHttpReverseProxy {
                 if (ex != null) {
                     System.out.println("[client<-proxy] " + httpExchange.getId() + " " + ex);
                     final int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
-                    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, status,
+                    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, status,
                             EnglishReasonPhraseCatalog.INSTANCE.getReason(status, Locale.US));
                     String message = ex.getMessage();
                     if (message == null) {
                         message = "Unexpected error";
                     }
-                    response.setEntity(new NStringEntity(message, ContentType.DEFAULT_TEXT));
-                    responseTrigger.submitResponse(new BasicAsyncResponseProducer(response));
+                    responseTrigger.submitResponse(new ErrorResponseProducer(response,
+                            new NStringEntity(message, ContentType.DEFAULT_TEXT), false));
                     System.out.println("[client<-proxy] " + httpExchange.getId() + " error response triggered");
                 }
                 final HttpResponse response = httpExchange.getResponse();
@@ -679,15 +677,16 @@ public class NHttpReverseProxy {
                 final HttpAsyncExchange responseTrigger = this.httpExchange.getResponseTrigger();
                 if (responseTrigger != null && !responseTrigger.isCompleted()) {
                     System.out.println("[client<-proxy] " + this.httpExchange.getId() + " " + ex);
-                    final int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
-                    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, status,
+                    final int status = ex instanceof SocketTimeoutException ?
+                            HttpStatus.SC_GATEWAY_TIMEOUT : HttpStatus.SC_INTERNAL_SERVER_ERROR;
+                    final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, status,
                             EnglishReasonPhraseCatalog.INSTANCE.getReason(status, Locale.US));
                     String message = ex.getMessage();
                     if (message == null) {
                         message = "Unexpected error";
                     }
-                    response.setEntity(new NStringEntity(message, ContentType.DEFAULT_TEXT));
-                    responseTrigger.submitResponse(new BasicAsyncResponseProducer(response));
+                    responseTrigger.submitResponse(new ErrorResponseProducer(response,
+                            new NStringEntity(message, ContentType.DEFAULT_TEXT), false));
                 }
             }
         }


[httpcomponents-core] 01/02: HTTPCORE-607: HttpAsyncService incorrectly attempts to submit a response over idle connection in case of a timeout (no prior request); cleaned up exception handling logic in HttpAsyncService; reverted HTTPCORE-482

Posted by ol...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch 4.4.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit f75d0de151f6a58e398cac930d2498b796a13e7d
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Oct 17 16:30:06 2019 +0200

    HTTPCORE-607: HttpAsyncService incorrectly attempts to submit a response over idle connection in case of a timeout (no prior request); cleaned up exception handling logic in HttpAsyncService; reverted HTTPCORE-482
---
 .../impl/nio/DefaultNHttpServerConnection.java     |   1 -
 .../apache/http/nio/protocol/HttpAsyncService.java | 110 ++++++++++++---------
 .../http/nio/protocol/TestHttpAsyncService.java    |  19 +++-
 3 files changed, 82 insertions(+), 48 deletions(-)

diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
index 8d7ee6b..30fe46f 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/DefaultNHttpServerConnection.java
@@ -290,7 +290,6 @@ public class DefaultNHttpServerConnection
                 }
             }
         } catch (final HttpException ex) {
-            resetInput();
             handler.exception(this, ex);
         } catch (final Exception ex) {
             handler.exception(this, ex);
diff --git a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
index ebeed8b..709dbf8 100644
--- a/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
+++ b/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncService.java
@@ -28,6 +28,7 @@
 package org.apache.http.nio.protocol;
 
 import java.io.IOException;
+import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -285,39 +286,67 @@ public class HttpAsyncService implements NHttpServerEventHandler {
         }
         state.setTerminated();
         closeHandlers(state, cause);
-        final Cancellable cancellable = state.getCancellable();
-        if (cancellable != null) {
-            cancellable.cancel();
-        }
-        final Queue<PipelineEntry> pipeline = state.getPipeline();
-        if (!pipeline.isEmpty()
-                || conn.isResponseSubmitted()
-                || state.getResponseState().compareTo(MessageState.INIT) > 0) {
-            // There is not much that we can do if a response
-            // has already been submitted or pipelining is being used.
-            shutdownConnection(conn);
-        } else {
-            try {
+        try {
+            final Cancellable cancellable = state.getCancellable();
+            if (cancellable != null) {
+                cancellable.cancel();
+            }
+            if (cause instanceof SocketException) {
+                // Transport layer is likely unreliable.
+                conn.shutdown();
+                return;
+            }
+            if (cause instanceof SocketTimeoutException) {
+                // Connection timed out due to inactivity.
+                conn.close();
+                return;
+            }
+
+            if (conn.isResponseSubmitted() || state.getResponseState().compareTo(MessageState.INIT) > 0) {
+                // There is not much that we can do if a response has already been submitted.
+                conn.close();
+                return;
+            }
+            HttpRequest request = conn.getHttpRequest();
+            if (request == null) {
                 final Incoming incoming = state.getIncoming();
-                final HttpRequest request = incoming != null ? incoming.getRequest() : null;
-                final HttpContext context = incoming != null ? incoming.getContext() : new BasicHttpContext();
+                if (incoming != null) {
+                    request = incoming.getRequest();
+                }
+            }
+            if (request == null) {
+                final Queue<PipelineEntry> pipeline = state.getPipeline();
+                final PipelineEntry pipelineEntry = pipeline.poll();
+                if (pipelineEntry != null) {
+                    request = pipelineEntry.getRequest();
+                }
+            }
+            if (request != null) {
+                conn.resetInput();
+                final HttpCoreContext context = HttpCoreContext.create();
                 final HttpAsyncResponseProducer responseProducer = handleException(cause, context);
                 final HttpResponse response = responseProducer.generateResponse();
                 final Outgoing outgoing = new Outgoing(request, response, responseProducer, context);
                 state.setResponseState(MessageState.INIT);
                 state.setOutgoing(outgoing);
                 commitFinalResponse(conn, state);
-            } catch (final Exception ex) {
-                shutdownConnection(conn);
-                closeHandlers(state);
-                if (ex instanceof RuntimeException) {
-                    throw (RuntimeException) ex;
-                }
-                log(ex);
+                return;
             }
+            conn.close();
+        } catch (final Exception ex) {
+            shutdownConnection(conn);
+            closeHandlers(state);
+            if (ex instanceof RuntimeException) {
+                throw (RuntimeException) ex;
+            }
+            log(ex);
         }
     }
 
+    protected HttpResponse createHttpResponse(final int status, final HttpContext context) {
+        return this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, status, context);
+    }
+
     @Override
     public void requestReceived(
             final NHttpServerConnection conn) throws IOException, HttpException {
@@ -350,8 +379,7 @@ public class HttpAsyncService implements NHttpServerEventHandler {
                         && !(conn instanceof SessionBufferStatus && ((SessionBufferStatus) conn).hasBufferedInput())) {
 
                 state.setRequestState(MessageState.ACK_EXPECTED);
-                final HttpResponse ack = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
-                        HttpStatus.SC_CONTINUE, context);
+                final HttpResponse ack = createHttpResponse(HttpStatus.SC_CONTINUE, context);
                 if (this.expectationVerifier != null) {
                     conn.suspendInput();
                     conn.suspendOutput();
@@ -442,11 +470,10 @@ public class HttpAsyncService implements NHttpServerEventHandler {
                 final Object result = pipelineEntry.getResult();
                 final HttpRequest request = pipelineEntry.getRequest();
                 final HttpContext context = pipelineEntry.getContext();
+                final HttpResponse response = createHttpResponse(HttpStatus.SC_OK, context);
+                final HttpAsyncExchangeImpl httpExchange = new HttpAsyncExchangeImpl(
+                        request, response, state, conn, context);
                 if (result != null) {
-                    final HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
-                            HttpStatus.SC_OK, context);
-                    final HttpAsyncExchangeImpl httpExchange = new HttpAsyncExchangeImpl(
-                            request, response, state, conn, context);
                     final HttpAsyncRequestHandler<Object> handler = pipelineEntry.getHandler();
                     conn.suspendOutput();
                     try {
@@ -454,14 +481,12 @@ public class HttpAsyncService implements NHttpServerEventHandler {
                     } catch (final RuntimeException ex) {
                         throw ex;
                     } catch (final Exception ex) {
-                        pipeline.add(new PipelineEntry(
-                            request,
-                            null,
-                            ex,
-                            handler,
-                            context));
-                        state.setResponseState(MessageState.READY);
-                        responseReady(conn);
+                        if (!httpExchange.isCompleted()) {
+                            httpExchange.submitResponse(handleException(ex, context));
+                        } else {
+                            log(ex);
+                            conn.close();
+                        }
                         return;
                     }
                 } else {
@@ -469,8 +494,7 @@ public class HttpAsyncService implements NHttpServerEventHandler {
                     final HttpAsyncResponseProducer responseProducer = handleException(
                             exception != null ? exception : new HttpException("Internal error processing request"),
                             context);
-                    final HttpResponse error = responseProducer.generateResponse();
-                    state.setOutgoing(new Outgoing(request, error, responseProducer, context));
+                    httpExchange.submitResponse(responseProducer);
                 }
             }
             if (state.getResponseState() == MessageState.INIT) {
@@ -529,7 +553,7 @@ public class HttpAsyncService implements NHttpServerEventHandler {
     public void timeout(final NHttpServerConnection conn) throws IOException {
         final State state = getState(conn);
         if (state != null) {
-            exception(conn, new SocketTimeoutException(
+            closeHandlers(state, new SocketTimeoutException(
                     String.format("%,d milliseconds timeout on connection %s", conn.getSocketTimeout(), conn)));
         }
         if (conn.getStatus() == NHttpConnection.ACTIVE) {
@@ -623,10 +647,8 @@ public class HttpAsyncService implements NHttpServerEventHandler {
         if (message == null) {
             message = ex.toString();
         }
-        final HttpResponse response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_1,
-                toStatusCode(ex, context), context);
-        return new ErrorResponseProducer(response,
-                new NStringEntity(message, ContentType.DEFAULT_TEXT), false);
+        final HttpResponse response = createHttpResponse(toStatusCode(ex, context), context);
+        return new ErrorResponseProducer(response, new NStringEntity(message, ContentType.DEFAULT_TEXT), false);
     }
 
     protected int toStatusCode(final Exception ex, final HttpContext context) {
@@ -637,8 +659,6 @@ public class HttpAsyncService implements NHttpServerEventHandler {
             code = HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED;
         } else if (ex instanceof ProtocolException) {
             code = HttpStatus.SC_BAD_REQUEST;
-        } else if (ex instanceof SocketTimeoutException) {
-            code = HttpStatus.SC_GATEWAY_TIMEOUT;
         } else {
             code = HttpStatus.SC_INTERNAL_SERVER_ERROR;
         }
diff --git a/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java b/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java
index dcb92a6..8ab870c 100644
--- a/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java
+++ b/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncService.java
@@ -163,6 +163,7 @@ public class TestHttpAsyncService {
         state.setIncoming(incoming);
         state.setCancellable(this.cancellable);
         this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
+        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
 
         final HttpException httpex = new HttpException();
         this.protocolHandler.exception(this.conn, httpex);
@@ -210,6 +211,7 @@ public class TestHttpAsyncService {
         state.setOutgoing(outgoing);
         state.setCancellable(this.cancellable);
         this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
+        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
 
         Mockito.doThrow(new RuntimeException()).when(this.httpProcessor).process(
                 Matchers.any(HttpResponse.class), Matchers.any(HttpContext.class));
@@ -243,6 +245,7 @@ public class TestHttpAsyncService {
         state.setOutgoing(outgoing);
         state.setCancellable(this.cancellable);
         this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
+        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
 
         Mockito.doThrow(new IOException()).when(this.httpProcessor).process(
                 Matchers.any(HttpResponse.class), Matchers.any(HttpContext.class));
@@ -280,7 +283,7 @@ public class TestHttpAsyncService {
 
         Assert.assertEquals(MessageState.READY, state.getRequestState());
         Assert.assertEquals(MessageState.READY, state.getResponseState());
-        Mockito.verify(this.conn).shutdown();
+        Mockito.verify(this.conn).close();
         Mockito.verify(this.requestConsumer).failed(httpex);
         Mockito.verify(this.requestConsumer).close();
         Mockito.verify(this.responseProducer).failed(httpex);
@@ -1329,19 +1332,31 @@ public class TestHttpAsyncService {
     @Test
     public void testTimeoutActiveConnection() throws Exception {
         final State state = new State();
+        final HttpContext exchangeContext = new BasicHttpContext();
+        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        final Incoming incoming = new Incoming(
+                request, this.requestHandler, this.requestConsumer, exchangeContext);
+        state.setIncoming(incoming);
         this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
+        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
         Mockito.when(this.conn.getStatus()).thenReturn(NHttpConnection.ACTIVE, NHttpConnection.CLOSED);
 
         this.protocolHandler.timeout(this.conn);
 
-        Mockito.verify(this.conn).close();
+        Mockito.verify(this.conn, Mockito.atLeastOnce()).close();
         Mockito.verify(this.conn, Mockito.never()).setSocketTimeout(Matchers.anyInt());
     }
 
     @Test
     public void testTimeoutActiveConnectionBufferedData() throws Exception {
         final State state = new State();
+        final HttpContext exchangeContext = new BasicHttpContext();
+        final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        final Incoming incoming = new Incoming(
+                request, this.requestHandler, this.requestConsumer, exchangeContext);
+        state.setIncoming(incoming);
         this.connContext.setAttribute(HttpAsyncService.HTTP_EXCHANGE_STATE, state);
+        Mockito.when(this.conn.getHttpRequest()).thenReturn(request);
         Mockito.when(this.conn.getStatus()).thenReturn(NHttpConnection.ACTIVE, NHttpConnection.CLOSING);
 
         this.protocolHandler.timeout(this.conn);