From 2196eaa312308d105b8990b450039f413a5de3e7 Mon Sep 17 00:00:00 2001
From: Julian Andres Klode <julian.klode@canonical.com>
Date: Mon, 29 Jun 2020 11:48:28 +0200
Subject: [PATCH] http: Die(): Do not flush the buffer, error out instead

By changing the buffer implementation to return true if it
read or wrote something, even on EOF, we should not have a
need to flush the buffer in Die() anymore - we should only
be calling Die() if the buffer is empty now.
---
 methods/http.cc | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/methods/http.cc b/methods/http.cc
index 07cbea9..357ae69 100644
--- a/methods/http.cc
+++ b/methods/http.cc
@@ -700,29 +700,14 @@ ResultState HttpServerState::Die(RequestState &Req)
    // Dump the buffer to the file
    if (Req.State == RequestState::Data)
    {
-      if (Req.File.IsOpen() == false)
-	 return ResultState::SUCCESSFUL;
       // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking
       // can't be set
       if (Req.File.Name() != "/dev/null")
 	 SetNonBlock(Req.File.Fd(),false);
-      if (not In.WriteSpace())
-	 return ResultState::SUCCESSFUL;
-      while (In.WriteSpace() == true)
-      {
-	 if (In.Write(MethodFd::FromFd(Req.File.Fd())) == false)
-	 {
-	    _error->Errno("write", _("Error writing to the file"));
-	    return ResultState::TRANSIENT_ERROR;
-	 }
-
-	 // Done
-	 if (In.IsLimit() == true)
-	    return ResultState::SUCCESSFUL;
+      if (In.WriteSpace()) {
+	 _error->Error(_("Data left in buffer"));
+	 return ResultState::TRANSIENT_ERROR;
       }
-
-      if (In.IsLimit() == true || Persistent == false)
-	 return ResultState::SUCCESSFUL;
    }
 
    // See if this is because the server finished the data stream
-- 
GitLab