add more logging to http download connection and reset file params after setting expected size

This commit is contained in:
Daniel Gultsch 2017-08-04 11:58:12 +02:00
parent 5cee46cda5
commit b925f436fd
4 changed files with 59 additions and 42 deletions

View file

@ -695,6 +695,10 @@ public class Message extends AbstractEntity {
return isGeoUri; return isGeoUri;
} }
public synchronized void resetFileParams() {
this.fileParams = null;
}
public synchronized FileParams getFileParams() { public synchronized FileParams getFileParams() {
if (fileParams == null) { if (fileParams == null) {
fileParams = new FileParams(); fileParams = new FileParams();

View file

@ -76,7 +76,7 @@ public class HttpDownloadConnection implements Transferable {
} }
String[] parts = mUrl.getPath().toLowerCase().split("\\."); String[] parts = mUrl.getPath().toLowerCase().split("\\.");
String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null; String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
String secondToLast = parts.length >= 2 ? parts[parts.length -2] : null; String secondToLast = parts.length >= 2 ? parts[parts.length - 2] : null;
if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) { if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
this.message.setEncryption(Message.ENCRYPTION_PGP); this.message.setEncryption(Message.ENCRYPTION_PGP);
} else if (message.getEncryption() != Message.ENCRYPTION_OTR } else if (message.getEncryption() != Message.ENCRYPTION_OTR
@ -149,11 +149,35 @@ public class HttpDownloadConnection implements Transferable {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
} else if (e instanceof FileWriterException) { } else if (e instanceof FileWriterException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
} else if (!(e instanceof CancellationException)) { } else if (!(e instanceof CancellationException)) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
} }
} }
public void updateProgress(long i) {
this.mProgress = (int) i;
mHttpConnectionManager.updateConversationUi(false);
}
@Override
public int getStatus() {
return this.mStatus;
}
@Override
public long getFileSize() {
if (this.file != null) {
return this.file.getExpectedSize();
} else {
return 0;
}
}
@Override
public int getProgress() {
return this.mProgress;
}
private class FileSizeChecker implements Runnable { private class FileSizeChecker implements Runnable {
private boolean interactive = false; private boolean interactive = false;
@ -180,6 +204,7 @@ public class HttpDownloadConnection implements Transferable {
return; return;
} }
file.setExpectedSize(size); file.setExpectedSize(size);
message.resetFileParams();
if (mHttpConnectionManager.hasStoragePermission() if (mHttpConnectionManager.hasStoragePermission()
&& size <= mHttpConnectionManager.getAutoAcceptFileSize() && size <= mHttpConnectionManager.getAutoAcceptFileSize()
&& mXmppConnectionService.isDataSaverDisabled()) { && mXmppConnectionService.isDataSaverDisabled()) {
@ -203,8 +228,8 @@ public class HttpDownloadConnection implements Transferable {
connection = (HttpURLConnection) mUrl.openConnection(); connection = (HttpURLConnection) mUrl.openConnection();
} }
connection.setRequestMethod("HEAD"); connection.setRequestMethod("HEAD");
Log.d(Config.LOGTAG,"url: "+connection.getURL().toString()); Log.d(Config.LOGTAG, "url: " + connection.getURL().toString());
Log.d(Config.LOGTAG,"connection: "+connection.toString()); Log.d(Config.LOGTAG, "connection: " + connection.toString());
connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getIdentityName()); connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getIdentityName());
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
@ -219,7 +244,7 @@ public class HttpDownloadConnection implements Transferable {
} }
return Long.parseLong(contentLength, 10); return Long.parseLong(contentLength, 10);
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG,"io exception during HEAD "+e.getMessage()); Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
throw e; throw e;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IOException(); throw new IOException();
@ -258,10 +283,10 @@ public class HttpDownloadConnection implements Transferable {
} }
} }
private void download() throws Exception { private void download() throws Exception {
InputStream is = null; InputStream is = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_"+message.getUuid()); PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid());
try { try {
wakeLock.acquire(); wakeLock.acquire();
if (mUseTor) { if (mUseTor) {
@ -272,31 +297,40 @@ public class HttpDownloadConnection implements Transferable {
if (connection instanceof HttpsURLConnection) { if (connection instanceof HttpsURLConnection) {
mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive); mHttpConnectionManager.setupTrustManager((HttpsURLConnection) connection, interactive);
} }
connection.setRequestProperty("User-Agent",mXmppConnectionService.getIqGenerator().getIdentityName()); connection.setRequestProperty("User-Agent", mXmppConnectionService.getIqGenerator().getIdentityName());
final boolean tryResume = file.exists() && file.getKey() == null; final boolean tryResume = file.exists() && file.getKey() == null && file.getSize() > 0;
long resumeSize = 0; long resumeSize = 0;
long expected = file.getExpectedSize();
if (tryResume) { if (tryResume) {
Log.d(Config.LOGTAG,"http download trying resume");
resumeSize = file.getSize(); resumeSize = file.getSize();
connection.setRequestProperty("Range", "bytes="+resumeSize+"-"); Log.d(Config.LOGTAG, "http download trying resume after" + resumeSize + " of " + expected);
connection.setRequestProperty("Range", "bytes=" + resumeSize + "-");
} }
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000); connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000); connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
connection.connect(); connection.connect();
is = new BufferedInputStream(connection.getInputStream()); is = new BufferedInputStream(connection.getInputStream());
final String contentRange = connection.getHeaderField("Content-Range"); final String contentRange = connection.getHeaderField("Content-Range");
boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes "+resumeSize+"-"); boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
long transmitted = 0; long transmitted = 0;
long expected = file.getExpectedSize();
if (tryResume && serverResumed) { if (tryResume && serverResumed) {
Log.d(Config.LOGTAG,"server resumed"); Log.d(Config.LOGTAG, "server resumed");
transmitted = file.getSize(); transmitted = file.getSize();
updateProgress((int) ((((double) transmitted) / expected) * 100)); updateProgress(Math.round(((double) transmitted / expected) * 100));
os = AbstractConnectionManager.createAppendedOutputStream(file); os = AbstractConnectionManager.createAppendedOutputStream(file);
if (os == null) { if (os == null) {
throw new FileWriterException(); throw new FileWriterException();
} }
} else { } else {
long reportedContentLengthOnGet;
try {
reportedContentLengthOnGet = Long.parseLong(connection.getHeaderField("Content-Length"));
} catch (NumberFormatException | NullPointerException e) {
reportedContentLengthOnGet = 0;
}
if (expected != reportedContentLengthOnGet) {
Log.d(Config.LOGTAG, "content-length reported on GET (" + reportedContentLengthOnGet + ") did not match Content-Length reported on HEAD (" + expected + ")");
}
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
if (!file.exists() && !file.createNewFile()) { if (!file.exists() && !file.createNewFile()) {
throw new FileWriterException(); throw new FileWriterException();
@ -304,7 +338,7 @@ public class HttpDownloadConnection implements Transferable {
os = AbstractConnectionManager.createOutputStream(file, true); os = AbstractConnectionManager.createOutputStream(file, true);
} }
int count; int count;
byte[] buffer = new byte[1024]; byte[] buffer = new byte[4096];
while ((count = is.read(buffer)) != -1) { while ((count = is.read(buffer)) != -1) {
transmitted += count; transmitted += count;
try { try {
@ -312,7 +346,7 @@ public class HttpDownloadConnection implements Transferable {
} catch (IOException e) { } catch (IOException e) {
throw new FileWriterException(); throw new FileWriterException();
} }
updateProgress((int) ((((double) transmitted) / expected) * 100)); updateProgress(Math.round(((double) transmitted / expected) * 100));
if (canceled) { if (canceled) {
throw new CancellationException(); throw new CancellationException();
} }
@ -323,7 +357,7 @@ public class HttpDownloadConnection implements Transferable {
throw new FileWriterException(); throw new FileWriterException();
} }
} catch (CancellationException | IOException e) { } catch (CancellationException | IOException e) {
Log.d(Config.LOGTAG,"http download failed "+e.getMessage()); Log.d(Config.LOGTAG, "http download failed " + e.getMessage());
throw e; throw e;
} finally { } finally {
FileBackend.close(os); FileBackend.close(os);
@ -349,28 +383,4 @@ public class HttpDownloadConnection implements Transferable {
} }
} }
public void updateProgress(int i) {
this.mProgress = i;
mHttpConnectionManager.updateConversationUi(false);
}
@Override
public int getStatus() {
return this.mStatus;
}
@Override
public long getFileSize() {
if (this.file != null) {
return this.file.getExpectedSize();
} else {
return 0;
}
}
@Override
public int getProgress() {
return this.mProgress;
}
} }

View file

@ -118,6 +118,7 @@ public class HttpUploadConnection implements Transferable {
return; return;
} }
this.file.setExpectedSize(pair.second); this.file.setExpectedSize(pair.second);
message.resetFileParams();
this.mFileInputStream = pair.first; this.mFileInputStream = pair.first;
Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD); Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD);
IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime); IqPacket request = mXmppConnectionService.getIqGenerator().requestHttpUploadSlot(host,file,mime);

View file

@ -460,6 +460,7 @@ public class JingleConnection implements Transferable {
} }
this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file,message.getEncryption() == Message.ENCRYPTION_AXOLOTL); this.mFileOutputStream = AbstractConnectionManager.createOutputStream(this.file,message.getEncryption() == Message.ENCRYPTION_AXOLOTL);
this.file.setExpectedSize(size); this.file.setExpectedSize(size);
message.resetFileParams();
Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize()); Log.d(Config.LOGTAG, "receiving file: expecting size of " + this.file.getExpectedSize());
} else { } else {
this.sendCancel(); this.sendCancel();
@ -504,6 +505,7 @@ public class JingleConnection implements Transferable {
cancel(); cancel();
return; return;
} }
message.resetFileParams();
this.mFileInputStream = pair.first; this.mFileInputStream = pair.first;
content.setTransportId(this.transportId); content.setTransportId(this.transportId);
content.socks5transport().setChildren(getCandidatesAsElements()); content.socks5transport().setChildren(getCandidatesAsElements());