Skip to content

Close input stream in AabUpdater #3282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,41 @@ UpdateTaskImpl updateAab(@NonNull AppDistributionReleaseInternal newRelease) {

private String fetchDownloadRedirectUrl(String downloadUrl)
throws FirebaseAppDistributionException {
HttpsURLConnection httpsURLConnection;
HttpsURLConnection connection = null;
int responseCode;
String redirectUrl;

try {
httpsURLConnection = httpsUrlConnectionFactory.openConnection(downloadUrl);
httpsURLConnection.setInstanceFollowRedirects(false);
responseCode = httpsURLConnection.getResponseCode();
connection = httpsUrlConnectionFactory.openConnection(downloadUrl);
connection.setInstanceFollowRedirects(false);
responseCode = connection.getResponseCode();
redirectUrl = connection.getHeaderField("Location");
// Prevents a {@link LeakedClosableViolation} in strict mode even when the connection is
// disconnected
connection.getInputStream().close();
} catch (IOException e) {
throw new FirebaseAppDistributionException(
"Failed to open connection to: " + downloadUrl, NETWORK_FAILURE, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}

if (!isRedirectResponse(responseCode)) {
throw new FirebaseAppDistributionException(
"Expected redirect response code, but got: " + responseCode, DOWNLOAD_FAILURE);
}
String redirect = httpsURLConnection.getHeaderField("Location");
httpsURLConnection.disconnect();

if (redirect == null) {
if (redirectUrl == null) {
throw new FirebaseAppDistributionException(
"No Location header found in response from: " + downloadUrl, DOWNLOAD_FAILURE);
} else if (redirect.isEmpty()) {
} else if (redirectUrl.isEmpty()) {
throw new FirebaseAppDistributionException(
"Empty Location header found in response from: " + downloadUrl, DOWNLOAD_FAILURE);
}

return redirect;
return redirectUrl;
}

private static boolean isRedirectResponse(int responseCode) {
Expand Down