Blame SOURCES/Always-buffer-TCP-data-in-__handle_recv.patch

3fdeea
From c1be487bb00f2e813212031d93fcebbfbd0da60b Mon Sep 17 00:00:00 2001
3fdeea
From: Robbie Harwood <rharwood@redhat.com>
3fdeea
Date: Thu, 29 Aug 2019 11:13:41 -0400
3fdeea
Subject: [PATCH] Always buffer TCP data in __handle_recv()
3fdeea
3fdeea
Refactor __handle_recv() to always create a BytesIO() object for TCP
3fdeea
data.  Linearize control flow for ease of debugging.  Always apply
3fdeea
length checks so that we don't have to wait for EOF in the multiple-recv
3fdeea
case.
3fdeea
3fdeea
Fixes a bug where we wouldn't return any data because we never received
3fdeea
the EOF, or didn't receive it fast enough.
3fdeea
3fdeea
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
3fdeea
(cherry picked from commit 7e2b1ab27b843c220fe301b74bab01ed61b0f59a)
3fdeea
---
3fdeea
 kdcproxy/__init__.py | 54 +++++++++++++++++++++++++-------------------
3fdeea
 1 file changed, 31 insertions(+), 23 deletions(-)
3fdeea
3fdeea
diff --git a/kdcproxy/__init__.py b/kdcproxy/__init__.py
3fdeea
index 6526bc9..9bc7044 100644
3fdeea
--- a/kdcproxy/__init__.py
3fdeea
+++ b/kdcproxy/__init__.py
3fdeea
@@ -128,29 +128,37 @@ class Application:
3fdeea
             # length prefix. So add it.
3fdeea
             reply = struct.pack("!I", len(reply)) + reply
3fdeea
             return reply
3fdeea
-        else:
3fdeea
-            # TCP is a different story. The reply must be buffered
3fdeea
-            # until the full answer is accumulated.
3fdeea
-            buf = read_buffers.get(sock)
3fdeea
-            part = sock.recv(1048576)
3fdeea
-            if buf is None:
3fdeea
-                if len(part) > 4:
3fdeea
-                    # got enough data in the initial package. Now check
3fdeea
-                    # if we got the full package in the first run.
3fdeea
-                    (length, ) = struct.unpack("!I", part[0:4])
3fdeea
-                    if length + 4 == len(part):
3fdeea
-                        return part
3fdeea
-                read_buffers[sock] = buf = io.BytesIO()
3fdeea
-
3fdeea
-            if part:
3fdeea
-                # data received, accumulate it in a buffer
3fdeea
-                buf.write(part)
3fdeea
-                return None
3fdeea
-            else:
3fdeea
-                # EOF received
3fdeea
-                read_buffers.pop(sock)
3fdeea
-                reply = buf.getvalue()
3fdeea
-                return reply
3fdeea
+
3fdeea
+        # TCP is a different story. The reply must be buffered until the full
3fdeea
+        # answer is accumulated.
3fdeea
+        buf = read_buffers.get(sock)
3fdeea
+        if buf is None:
3fdeea
+            read_buffers[sock] = buf = io.BytesIO()
3fdeea
+
3fdeea
+        part = sock.recv(1048576)
3fdeea
+        if not part:
3fdeea
+            # EOF received.  Return any incomplete data we have on the theory
3fdeea
+            # that a decode error is more apparent than silent failure.  The
3fdeea
+            # client will fail faster, at least.
3fdeea
+            read_buffers.pop(sock)
3fdeea
+            reply = buf.getvalue()
3fdeea
+            return reply
3fdeea
+
3fdeea
+        # Data received, accumulate it in a buffer.
3fdeea
+        buf.write(part)
3fdeea
+
3fdeea
+        reply = buf.getvalue()
3fdeea
+        if len(reply) < 4:
3fdeea
+            # We don't have the length yet.
3fdeea
+            return None
3fdeea
+
3fdeea
+        # Got enough data to check if we have the full package.
3fdeea
+        (length, ) = struct.unpack("!I", reply[0:4])
3fdeea
+        if length + 4 == len(reply):
3fdeea
+            read_buffers.pop(sock)
3fdeea
+            return reply
3fdeea
+
3fdeea
+        return None
3fdeea
 
3fdeea
     def __filter_addr(self, addr):
3fdeea
         if addr[0] not in (socket.AF_INET, socket.AF_INET6):