am c83a3a84: am 0a910e21: Connection: close is enough to know length. automerge:...
Jeff Sharkey [Mon, 10 Nov 2014 22:42:55 +0000 (22:42 +0000)]
* commit 'c83a3a844ec0c3d7a000fbf26a181bff1ddeb8a3':
  Connection: close is enough to know length.

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");
         }