Merge branch 'fix-430--download-without-content-length' into 'master'

Ddownload index, even when not presented with a Content-Length header (Fixes 430)

This works for the index download, but still does not work for downloading .apks correctly. That is, the Android Download Manager needs one of:

 * `Content-Length: ...`
 * `Connection: close`
 * `Transfer-Encoding: Chunked`

headers to be set to work correctly. In the absence of all three of these, problems ensue. In fact in my toy web server, even the `Connection: close` did not seem to work correctly, but I'd be happy to be corrected on that. Either way, this is an improvement on what was there before.

For reference, here is my toy PHP web server, which can be saved in the root of the F-Droid repo and invoked with:

> `php -S 10.0.0.4:8888 no-headers.php`

```
<?php

function streamFile( $file ) { $size     = filesize( $file );
        $contents = file_get_contents( $file );

        $buffer = (int)( $size / 5 );
        $bytes = 0;
        while ( $bytes < $size ) {
                $toStream = min( $size - $bytes, $buffer );
                echo substr( $contents, $bytes, $toStream );
                $bytes += $toStream;

                // Sleep to allow progress to be viewed in F-Droid
                sleep( 1 );
        }
}

$index    = "/fdroid/repo/index.jar";
$firefox  = "/fdroid/repo/fennec-40.0.multi.android-arm.apk";

// Test downloading a large .apk to see how it behaves
if ( $_SERVER['REQUEST_URI'] == "/fdroid/repo/fennec-40.0.multi.android-arm.apk" ) {
        $file = $firefox;
} else {
        $file = $index;
}

// Android Download Manager requires this (if not using Content-Length or Transfer-Encoding
// headers, but I can't seem to get it to work as expected).
header( "Connection: Close" );
streamFile( dirname( __FILE__ ) . $file );

```

See merge request !150
This commit is contained in:
Daniel Martí 2015-10-04 15:00:07 +00:00
commit d7a30b4c34
6 changed files with 38 additions and 30 deletions

View File

@ -172,6 +172,13 @@
- Percentage complete (int between 0-100)
-->
<string name="status_download">Downloading\n%2$s / %3$s (%4$d%%) from\n%1$s</string>
<!--
status_download_unknown_size takes two parameters:
- Repository (url)
- Downloaded size (human readable)
-->
<string name="status_download_unknown_size">Downloading\n%2$s from\n%1$s</string>
<string name="update_notification_title">Updating repositories</string>
<string name="status_processing_xml_percent">Processing %2$s / %3$s (%4$d%%) from %1$s</string>
<string name="status_connecting_to_repo">Connecting to\n%1$s</string>

View File

@ -1498,31 +1498,26 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
/**
* Updates progress bar and captions to new values (in bytes).
*/
public void updateProgress(long progress, long total) {
public void updateProgress(long bytesDownloaded, long totalBytes) {
if (bytesDownloaded < 0 || totalBytes == 0) {
// Avoid division by zero and other weird values
if (progress < 0 || total <= 0) {
return;
}
long percent = progress * 100 / total;
if (totalBytes == -1) {
setProgressVisible(true);
progressBar.setIndeterminate(true);
progressSize.setText(Utils.getFriendlySize(bytesDownloaded));
progressPercent.setText("");
} else {
long percent = bytesDownloaded * 100 / totalBytes;
setProgressVisible(true);
progressBar.setIndeterminate(false);
progressBar.setProgress((int) percent);
progressBar.setMax(100);
progressSize.setText(readableFileSize(progress) + " / " + readableFileSize(total));
progressSize.setText(Utils.getFriendlySize(bytesDownloaded) + " / " + Utils.getFriendlySize(totalBytes));
progressPercent.setText(Long.toString(percent) + " %");
}
/**
* Converts a number of bytes to a human readable file size (eg 3.5 GiB).
*
* Based on http://stackoverflow.com/a/5599842
*/
public String readableFileSize(long bytes) {
final String[] units = getResources().getStringArray(R.array.file_size_units);
if (bytes <= 0) return "0 " + units[0];
int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
return new DecimalFormat("#,##0.#")
.format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
/**

View File

@ -209,12 +209,17 @@ public class UpdateService extends IntentService implements ProgressListener {
String repoAddress = intent.getStringExtra(Downloader.EXTRA_ADDRESS);
int downloadedSize = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, -1);
String downloadedSizeFriendly = Utils.getFriendlySize(downloadedSize);
int totalSize = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, -1);
int percent = (int) ((double) downloadedSize / totalSize * 100);
sendStatus(STATUS_INFO,
getString(R.string.status_download, repoAddress,
Utils.getFriendlySize(downloadedSize),
Utils.getFriendlySize(totalSize), percent));
String message;
if (totalSize == -1) {
message = getString(R.string.status_download_unknown_size, repoAddress, downloadedSizeFriendly);
} else {
String totalSizeFriendly = Utils.getFriendlySize(totalSize);
message = getString(R.string.status_download, repoAddress, downloadedSizeFriendly, totalSizeFriendly, percent);
}
sendStatus(STATUS_INFO, message);
}
};

View File

@ -186,7 +186,7 @@ public final class Utils {
}
}
public static String getFriendlySize(int size) {
public static String getFriendlySize(long size) {
double s = size;
int i = 0;
while (i < FRIENDLY_SIZE_FORMAT.length - 1 && s >= 1024) {

View File

@ -154,11 +154,10 @@ public abstract class Downloader {
throwExceptionIfInterrupted();
sendProgress(bytesRead, totalBytes);
while (bytesRead < totalBytes) {
while (true) {
int count;
if (input.available()>0) {
if (input.available() > 0) {
int readLength = Math.min(input.available(), buffer.length);
count = input.read(buffer, 0, readLength);
} else {

View File

@ -293,6 +293,8 @@ public class SwapAppsView extends ListView implements
progressView.setIndeterminate(false);
progressView.setMax(100);
progressView.setProgress(progress);
} else {
progressView.setIndeterminate(true);
}
}
};