From 6c0a32c9e5ccccab321e984100020b3269aef456 Mon Sep 17 00:00:00 2001
From: Julian Andres Klode <julian.klode@canonical.com>
Date: Mon, 29 Jun 2020 12:23:02 +0200
Subject: [PATCH] http: Finish copying data from server to file before sending
 stuff to server

This avoids a case where we read data, then write to the server
and only then realize the connection was closed. It is somewhat
slower, though.
---
 methods/http.cc | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/methods/http.cc b/methods/http.cc
index 357ae69..16aaffc 100644
--- a/methods/http.cc
+++ b/methods/http.cc
@@ -841,13 +841,6 @@ ResultState HttpServerState::Go(bool ToFile, RequestState &Req)
 	 return Die(Req);
    }
 
-   if (ServerFd->Fd() != -1 && FD_ISSET(ServerFd->Fd(), &wfds))
-   {
-      errno = 0;
-      if (Out.Write(ServerFd) == false)
-	 return Die(Req);
-   }
-
    // Send data to the file
    if (FileFD->Fd() != -1 && FD_ISSET(FileFD->Fd(), &wfds))
    {
@@ -858,6 +851,13 @@ ResultState HttpServerState::Go(bool ToFile, RequestState &Req)
       }
    }
 
+   if (ServerFd->Fd() != -1 && FD_ISSET(ServerFd->Fd(), &wfds))
+   {
+      errno = 0;
+      if (Out.Write(ServerFd) == false)
+	 return Die(Req);
+   }
+
    if (Req.MaximumSize > 0 && Req.File.IsOpen() && Req.File.Failed() == false && Req.File.Tell() > Req.MaximumSize)
    {
       Owner->SetFailReason("MaximumSizeExceeded");
-- 
GitLab