Skip to content

Commit e1de9ed

Browse files
authored
Close input stream in AabUpdater (#3282)
1 parent e52d01a commit e1de9ed

File tree

1 file changed

+16
-9
lines changed
  • firebase-app-distribution/src/main/java/com/google/firebase/app/distribution

1 file changed

+16
-9
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/AabUpdater.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,41 @@ UpdateTaskImpl updateAab(@NonNull AppDistributionReleaseInternal newRelease) {
9292

9393
private String fetchDownloadRedirectUrl(String downloadUrl)
9494
throws FirebaseAppDistributionException {
95-
HttpsURLConnection httpsURLConnection;
95+
HttpsURLConnection connection = null;
9696
int responseCode;
97+
String redirectUrl;
9798

9899
try {
99-
httpsURLConnection = httpsUrlConnectionFactory.openConnection(downloadUrl);
100-
httpsURLConnection.setInstanceFollowRedirects(false);
101-
responseCode = httpsURLConnection.getResponseCode();
100+
connection = httpsUrlConnectionFactory.openConnection(downloadUrl);
101+
connection.setInstanceFollowRedirects(false);
102+
responseCode = connection.getResponseCode();
103+
redirectUrl = connection.getHeaderField("Location");
104+
// Prevents a {@link LeakedClosableViolation} in strict mode even when the connection is
105+
// disconnected
106+
connection.getInputStream().close();
102107
} catch (IOException e) {
103108
throw new FirebaseAppDistributionException(
104109
"Failed to open connection to: " + downloadUrl, NETWORK_FAILURE, e);
110+
} finally {
111+
if (connection != null) {
112+
connection.disconnect();
113+
}
105114
}
106115

107116
if (!isRedirectResponse(responseCode)) {
108117
throw new FirebaseAppDistributionException(
109118
"Expected redirect response code, but got: " + responseCode, DOWNLOAD_FAILURE);
110119
}
111-
String redirect = httpsURLConnection.getHeaderField("Location");
112-
httpsURLConnection.disconnect();
113120

114-
if (redirect == null) {
121+
if (redirectUrl == null) {
115122
throw new FirebaseAppDistributionException(
116123
"No Location header found in response from: " + downloadUrl, DOWNLOAD_FAILURE);
117-
} else if (redirect.isEmpty()) {
124+
} else if (redirectUrl.isEmpty()) {
118125
throw new FirebaseAppDistributionException(
119126
"Empty Location header found in response from: " + downloadUrl, DOWNLOAD_FAILURE);
120127
}
121128

122-
return redirect;
129+
return redirectUrl;
123130
}
124131

125132
private static boolean isRedirectResponse(int responseCode) {

0 commit comments

Comments
 (0)