Recover from CDN 416 Range error on attachment download.

This commit is contained in:
Alan Evans
2021-01-13 00:04:50 -04:00
committed by Greyson Parrelli
parent be91f2396c
commit adea15df10
3 changed files with 43 additions and 18 deletions

View File

@@ -0,0 +1,14 @@
/**
* Copyright (C) 2014-2016 Open Whisper Systems
* <p>
* Licensed according to the LICENSE file in this repository.
*/
package org.whispersystems.signalservice.api.push.exceptions;
public final class RangeException extends NonSuccessfulResponseCodeException {
public RangeException(long requested) {
super("Range request out of bounds " + requested);
}
}

View File

@@ -59,6 +59,7 @@ import org.whispersystems.signalservice.api.push.exceptions.NoContentException;
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.signalservice.api.push.exceptions.NotFoundException;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import org.whispersystems.signalservice.api.push.exceptions.RangeException;
import org.whispersystems.signalservice.api.push.exceptions.RateLimitException;
import org.whispersystems.signalservice.api.push.exceptions.RemoteAttestationResponseExpiredException;
import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
@@ -571,7 +572,8 @@ public class PushServiceSocket {
}
public void retrieveAttachment(int cdnNumber, SignalServiceAttachmentRemoteId cdnPath, File destination, long maxSizeBytes, ProgressListener listener)
throws NonSuccessfulResponseCodeException, PushNetworkException, MissingConfigurationException {
throws IOException, MissingConfigurationException
{
final String path;
if (cdnPath.getV2().isPresent()) {
path = String.format(Locale.US, ATTACHMENT_ID_DOWNLOAD_PATH, cdnPath.getV2().get());
@@ -581,12 +583,6 @@ public class PushServiceSocket {
downloadFromCdn(destination, cdnNumber, path, maxSizeBytes, listener);
}
public void retrieveSticker(File destination, byte[] packId, int stickerId)
throws NonSuccessfulResponseCodeException, PushNetworkException, MissingConfigurationException {
String hexPackId = Hex.toStringCondensed(packId);
downloadFromCdn(destination, 0, String.format(Locale.US, STICKER_PATH, hexPackId, stickerId), 1024 * 1024, null);
}
public byte[] retrieveSticker(byte[] packId, int stickerId)
throws NonSuccessfulResponseCodeException, PushNetworkException {
String hexPackId = Hex.toStringCondensed(packId);
@@ -694,7 +690,8 @@ public class PushServiceSocket {
}
public void retrieveProfileAvatar(String path, File destination, long maxSizeBytes)
throws NonSuccessfulResponseCodeException, PushNetworkException {
throws IOException
{
try {
downloadFromCdn(destination, 0, path, maxSizeBytes, null);
} catch (MissingConfigurationException e) {
@@ -971,11 +968,10 @@ public class PushServiceSocket {
}
private void downloadFromCdn(File destination, int cdnNumber, String path, long maxSizeBytes, ProgressListener listener)
throws PushNetworkException, NonSuccessfulResponseCodeException, MissingConfigurationException {
throws IOException, MissingConfigurationException
{
try (FileOutputStream outputStream = new FileOutputStream(destination, true)) {
downloadFromCdn(outputStream, destination.length(), cdnNumber, path, maxSizeBytes, listener);
} catch (IOException e) {
throw new PushNetworkException(e);
}
}
@@ -1037,13 +1033,17 @@ public class PushServiceSocket {
}
return;
} else if (response.code() == 416) {
throw new RangeException(offset);
}
} catch (NonSuccessfulResponseCodeException | PushNetworkException e) {
throw e;
} catch (IOException e) {
throw new PushNetworkException(e);
} finally {
if (body != null) {
body.close();
}
throw new PushNetworkException(e);
} finally {
synchronized (connections) {
connections.remove(call);
}