Connection: close is enough to know length.
Jeff Sharkey [Mon, 10 Nov 2014 18:06:14 +0000 (10:06 -0800)]
Now that we're defeating connection reuse, we have one additional
type of response where the length is known.

Bug: 18306491
Change-Id: I19657c565238f07fd89a55a5dbf1e85748f6e7c3

src/com/android/providers/downloads/DownloadThread.java

index c0f5a5e..2493ada 100644 (file)
@@ -439,12 +439,16 @@ public class DownloadThread implements Runnable {
      */
     private void transferData(HttpURLConnection conn) throws StopRequestException {
 
-        // To detect when we're really finished, we either need a length or
-        // chunked encoding.
+        // To detect when we're really finished, we either need a length, closed
+        // connection, or chunked encoding.
         final boolean hasLength = mInfoDelta.mTotalBytes != -1;
-        final String transferEncoding = conn.getHeaderField("Transfer-Encoding");
-        final boolean isChunked = "chunked".equalsIgnoreCase(transferEncoding);
-        if (!hasLength && !isChunked) {
+        final boolean isConnectionClose = "close".equalsIgnoreCase(
+                conn.getHeaderField("Connection"));
+        final boolean isEncodingChunked = "chunked".equalsIgnoreCase(
+                conn.getHeaderField("Transfer-Encoding"));
+
+        final boolean finishKnown = hasLength || isConnectionClose || isEncodingChunked;
+        if (!finishKnown) {
             throw new StopRequestException(
                     STATUS_CANNOT_RESUME, "can't know size of download, giving up");
         }