Skip to content

Commit 5217ee5

Browse files
authored
Merge pull request #316 from sxci/fix-crc32
check crc32
2 parents e60cb76 + d94d15d commit 5217ee5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

library/src/main/java/com/qiniu/android/http/ResponseInfo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public final class ResponseInfo {
2222
public static final int Cancelled = -2;
2323
public static final int NetworkError = -1;
2424

25+
public static final int Crc32NotMatch = -406;
26+
2527
public static final int UnknownError = 0;
2628

2729
// <-- error code copy from ios
@@ -171,6 +173,12 @@ private static String getUpType(String path) {
171173
}
172174
}
173175

176+
public static ResponseInfo errorInfo(ResponseInfo old, int statusCode, String error) {
177+
ResponseInfo _new = new ResponseInfo(old.response, statusCode, old.reqId, old.xlog, old.xvia, old.host,
178+
old.path, old.ip, old.port, old.duration, old.sent, error, old.upToken, old.totalSize);
179+
return _new;
180+
}
181+
174182
public static ResponseInfo zeroSize(final UpToken upToken) {
175183
return create(null, ZeroSizeFile, "", "", "", "", "", "", 80, 0, 0, "file or data size is zero", upToken, 0);
176184
}

library/src/main/java/com/qiniu/android/storage/ResumeUploader.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,36 @@ public void complete(ResponseInfo info, JSONObject response) {
301301
return;
302302
}
303303
long crc = 0;
304+
Exception tempE = null;
304305
try {
305306
context = response.getString("ctx");
306307
crc = response.getLong("crc32");
307308
} catch (Exception e) {
309+
tempE = e;
308310
e.printStackTrace();
309311
}
310312
if ((context == null || crc != ResumeUploader.this.crc32) && retried < config.retryMax) {
311313
String upHostRetry = config.zone.upHost(token.token, config.useHttps, upHost);
312314
nextTask(offset, retried + 1, upHostRetry);
313315
return;
314316
}
317+
if (context == null) {
318+
String error = "get context failed.";
319+
if (tempE != null) {
320+
error += "\n";
321+
error += tempE.getMessage();
322+
}
323+
ResponseInfo info2 = ResponseInfo.errorInfo(info, ResponseInfo.UnknownError, error);
324+
completionHandler.complete(key, info2, response);
325+
return;
326+
}
327+
if (crc != ResumeUploader.this.crc32) {
328+
String error = "block's crc32 is not match. local: " + ResumeUploader.this.crc32 + ", remote: " + crc;
329+
ResponseInfo info2 = ResponseInfo.errorInfo(info, ResponseInfo.Crc32NotMatch, error);
330+
completionHandler.complete(key, info2, response);
331+
return;
332+
}
333+
315334
contexts[(int) (offset / Configuration.BLOCK_SIZE)] = context;
316335
record(offset + chunkSize);
317336
nextTask(offset + chunkSize, retried, upHost);

0 commit comments

Comments
 (0)