From cd52b9f0da6ad17287e85e55363248d9f7430ba4 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Fri, 20 Aug 2004 12:09:09 +0000
Subject: [PATCH] Alexander Krasnostavsky made the write callback get called
 even when a zero byte file is downloaded.

---
 CHANGES         |  4 ++++
 RELEASE-NOTES   |  1 +
 docs/KNOWN_BUGS |  4 ----
 lib/transfer.c  | 22 +++++++++++++++-------
 4 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/CHANGES b/CHANGES
index 61fddfb4b2..8f4b864216 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,10 @@
 
                                   Changelog
 
+Daniel (20 August 2004)
+- Alexander Krasnostavsky made the write callback get called even when a zero
+  byte file is downloaded.
+
 Daniel (18 August 2004)
 - Ling Thio pointed out that when libcurl is built ipv6-enabled, it still did
   reverse DNS lookups when fed with a numerical IP-address (like
diff --git a/RELEASE-NOTES b/RELEASE-NOTES
index 68eb42ad37..b45dcc52a9 100644
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -14,6 +14,7 @@ This release includes the following changes:
 
 This release includes the following bugfixes:
 
+ o downloading empty files now calls the write callback properly
  o no more reverse DNS lookups when getting ip-only address with ipv6-enabled
    libcurl
  o libcurl works better multi-threaded on AIX (when built with xlc)
diff --git a/docs/KNOWN_BUGS b/docs/KNOWN_BUGS
index 7d9132781b..8a32559745 100644
--- a/docs/KNOWN_BUGS
+++ b/docs/KNOWN_BUGS
@@ -44,10 +44,6 @@ may have been fixed since this was written!
   libcurl thinks of it as the *compressed* lenght. Some explanations are here:
   http://curl.haxx.se/mail/lib-2003-06/0146.html
 
-* Downloading 0 (zero) bytes files over FTP will not create a zero byte file
-  locally, which is because libcurl doesn't call the write callback with zero
-  bytes. Explained here: http://curl.haxx.se/mail/archive-2003-04/0143.html
-
 * IPv6 support on AIX 4.3.3 doesn't work due to a missing sockaddr_storage
   struct. It has been reported to work on AIX 5.1 though.
 
diff --git a/lib/transfer.c b/lib/transfer.c
index 93df4c589a..7b22546a6b 100644
--- a/lib/transfer.c
+++ b/lib/transfer.c
@@ -250,7 +250,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
     if((k->keepon & KEEP_READ) &&
        (!readfdp || FD_ISSET(conn->sockfd, readfdp))) {
 
-      bool readdone = TRUE;
+      bool is_empty_data = FALSE;
 
       /* This is where we loop until we have read everything there is to
          read or we get a EWOULDBLOCK */
@@ -279,9 +279,11 @@ CURLcode Curl_readwrite(struct connectdata *conn,
         }
 
         didwhat |= KEEP_READ;
+        /* indicates data of zero size, i.e. empty file */
+        is_empty_data = (nread == 0 && k->bodywrites == 0);
 
         /* NULL terminate, allowing string ops to be used */
-        if (0 < nread)
+        if (0 < nread || is_empty_data)
           k->buf[nread] = 0;
 
         /* if we receive 0 or less here, the server closed the connection and
@@ -289,7 +291,6 @@ CURLcode Curl_readwrite(struct connectdata *conn,
         else if (0 >= nread) {
           k->keepon &= ~KEEP_READ;
           FD_ZERO(&k->rkeepfd);
-          readdone = TRUE;
           break;
         }
 
@@ -922,9 +923,9 @@ CURLcode Curl_readwrite(struct connectdata *conn,
         /* This is not an 'else if' since it may be a rest from the header
            parsing, where the beginning of the buffer is headers and the end
            is non-headers. */
-        if (k->str && !k->header && (nread > 0)) {
+        if (k->str && !k->header && (nread > 0 || is_empty_data)) {
 
-          if(0 == k->bodywrites) {
+          if(0 == k->bodywrites && !is_empty_data) {
             /* These checks are only made the first time we are about to
                write a piece of the body */
             if(conn->protocol&PROT_HTTP) {
@@ -1037,7 +1038,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
           Curl_pgrsSetDownloadCounter(data, k->bytecount);
 
-          if(!conn->bits.chunk && (nread || k->badheader)) {
+          if(!conn->bits.chunk && (nread || k->badheader || is_empty_data)) {
             /* If this is chunky transfer, it was already written */
 
             if(k->badheader && !k->ignorebody) {
@@ -1094,7 +1095,14 @@ CURLcode Curl_readwrite(struct connectdata *conn,
 
         } /* if (! header and data to read ) */
 
-      } while(!readdone);
+        if (is_empty_data) {
+          /* if we received nothing, the server closed the connection and we
+             are done */
+          k->keepon &= ~KEEP_READ;
+          FD_ZERO(&k->rkeepfd);
+        }
+
+      } while(0);
 
     } /* if( read from socket ) */
 
-- 
GitLab