Skip to content

Commit

Permalink
Only fetch http headers on checking for download file size
Browse files Browse the repository at this point in the history
(fixes #1683)
  • Loading branch information
Brutus5000 committed May 6, 2020
1 parent fb1ac5e commit b6ecb06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/main/java/com/faforever/client/update/CheckForUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

@Component
@Slf4j
Expand All @@ -21,18 +24,26 @@ public class CheckForUpdateTask extends CompletableTask<UpdateInfo> {

private final I18n i18n;
private final PreferencesService preferencesService;

@VisibleForTesting
FileSizeReader fileSizeReader = url -> url
.openConnection()
.getContentLength();

public CheckForUpdateTask(I18n i18n, PreferencesService preferencesService) {
super(Priority.LOW);
this.i18n = i18n;
this.preferencesService = preferencesService;
}

@VisibleForTesting
int getFileSize(URL url) throws IOException {
URLConnection connection = url.openConnection();
Assert.state(connection instanceof HttpURLConnection, "Unexpected connection type: " + connection.getClass());

HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("HEAD");

int fileSize = httpConnection.getContentLength();
httpConnection.disconnect();

return fileSize;
}

@Override
protected UpdateInfo call() throws Exception {
updateTitle(i18n.get("clientUpdateCheckTask.title"));
Expand Down Expand Up @@ -70,11 +81,6 @@ protected UpdateInfo call() throws Exception {
);
}

@SneakyThrows
private int getFileSize(URL downloadUrl) {
return fileSizeReader.read(downloadUrl);
}

// TODO make this available as a bean and use it in MapService as well
@VisibleForTesting
interface FileSizeReader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void testIsNewer() throws Exception {

clientProperties.setClientConfigUrl("http://" + LOOPBACK_ADDRESS.getHostAddress() + ":" + port);

instance.fileSizeReader = url -> 123;
UpdateInfo updateInfo = instance.call();

assertThat(updateInfo.getSize(), is(123));
Expand Down

0 comments on commit b6ecb06

Please sign in to comment.