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

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