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/09/25 09:42:42 UTC

[httpcomponents-core] 01/01: Simplification of HTTP/1.1 read event handling logic

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

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

commit 36e1c5e85f2eccd21d8761651b096cfd8a652188
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Sep 24 19:56:46 2019 +0200

    Simplification of HTTP/1.1 read event handling logic
---
 .../http/impl/nio/AbstractHttp1StreamDuplexer.java | 95 +++++++++++-----------
 1 file changed, 46 insertions(+), 49 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
index 6860bd3..4f715d3 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/AbstractHttp1StreamDuplexer.java
@@ -238,61 +238,51 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
         if (src != null) {
             inbuf.put(src);
         }
-        while (connState.compareTo(ConnectionState.SHUTDOWN) < 0) {
-            int totalBytesRead = 0;
-            int messagesReceived = 0;
+
+        if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0 && inbuf.hasData() && inputIdle()) {
+            ioSession.clearEvent(SelectionKey.OP_READ);
+            return;
+        }
+
+        boolean endOfStream = false;
+        if (incomingMessage == null) {
+            final int bytesRead = inbuf.fill(ioSession);
+            if (bytesRead > 0) {
+                inTransportMetrics.incrementBytesTransferred(bytesRead);
+            }
+            endOfStream = bytesRead == -1;
+        }
+
+        for (;;) {
             if (incomingMessage == null) {
 
-                if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0 && inputIdle()) {
-                    ioSession.clearEvent(SelectionKey.OP_READ);
-                    return;
-                }
+                final IncomingMessage messageHead = incomingMessageParser.parse(inbuf, endOfStream);
+                if (messageHead != null) {
+                    incomingMessageParser.reset();
 
-                int bytesRead;
-                do {
-                    bytesRead = inbuf.fill(ioSession);
-                    if (bytesRead > 0) {
-                        totalBytesRead += bytesRead;
-                        inTransportMetrics.incrementBytesTransferred(bytesRead);
+                    this.version = messageHead.getVersion();
+
+                    updateInputMetrics(messageHead, connMetrics);
+                    final ContentDecoder contentDecoder;
+                    if (handleIncomingMessage(messageHead)) {
+                        final long len = incomingContentStrategy.determineLength(messageHead);
+                        contentDecoder = createContentDecoder(len, ioSession, inbuf, inTransportMetrics);
+                        consumeHeader(messageHead, contentDecoder != null ? new IncomingEntityDetails(messageHead, len) : null);
+                    } else {
+                        consumeHeader(messageHead, null);
+                        contentDecoder = null;
                     }
-                    final IncomingMessage messageHead = incomingMessageParser.parse(inbuf, bytesRead == -1);
-                    if (messageHead != null) {
-                        messagesReceived++;
-                        incomingMessageParser.reset();
-
-                        this.version = messageHead.getVersion();
-
-                        updateInputMetrics(messageHead, connMetrics);
-                        final ContentDecoder contentDecoder;
-                        if (handleIncomingMessage(messageHead)) {
-                            final long len = incomingContentStrategy.determineLength(messageHead);
-                            contentDecoder = createContentDecoder(len, ioSession, inbuf, inTransportMetrics);
-                            consumeHeader(messageHead, contentDecoder != null ? new IncomingEntityDetails(messageHead, len) : null);
-                        } else {
-                            consumeHeader(messageHead, null);
-                            contentDecoder = null;
-                        }
-                        capacityWindow = new CapacityWindow(http1Config.getInitialWindowSize(), ioSession);
-                        if (contentDecoder != null) {
-                            incomingMessage = new Message<>(messageHead, contentDecoder);
-                            break;
-                        }
+                    capacityWindow = new CapacityWindow(http1Config.getInitialWindowSize(), ioSession);
+                    if (contentDecoder != null) {
+                        incomingMessage = new Message<>(messageHead, contentDecoder);
+                    } else {
                         inputEnd();
                         if (connState.compareTo(ConnectionState.ACTIVE) == 0) {
                             ioSession.setEvent(SelectionKey.OP_READ);
-                        } else {
-                            break;
                         }
                     }
-                } while (bytesRead > 0);
-
-                if (bytesRead == -1 && !inbuf.hasData()) {
-                    if (outputIdle() && inputIdle()) {
-                        requestShutdown(CloseMode.GRACEFUL);
-                    } else {
-                        shutdownSession(new ConnectionClosedException("Connection closed by peer"));
-                    }
-                    return;
+                } else {
+                    break;
                 }
             }
 
@@ -305,7 +295,6 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                 // input buffer
                 int bytesRead;
                 while ((bytesRead = contentDecoder.read(contentBuffer)) > 0) {
-                    totalBytesRead += bytesRead;
                     contentBuffer.flip();
                     consumeData(contentBuffer);
                     contentBuffer.clear();
@@ -324,9 +313,17 @@ abstract class AbstractHttp1StreamDuplexer<IncomingMessage extends HttpMessage,
                     ioSession.setEvent(SelectionKey.OP_READ);
                     inputEnd();
                 }
+                if (bytesRead == 0) {
+                    break;
+                }
             }
-            if (totalBytesRead == 0 && messagesReceived == 0) {
-                break;
+        }
+
+        if (endOfStream && !inbuf.hasData()) {
+            if (outputIdle() && inputIdle()) {
+                requestShutdown(CloseMode.GRACEFUL);
+            } else {
+                shutdownSession(new ConnectionClosedException("Connection closed by peer"));
             }
         }
     }