Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37d54d3

Browse files
authoredMar 11, 2018
Merge pull request lingochamp#968 from rantianhua/master
fix: cannot get content length during download connections
2 parents 0795595 + 1442f8c commit 37d54d3

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed
 

‎library/src/main/java/com/liulishuo/filedownloader/download/DownloadLaunchRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private void handleTrialConnectResult(Map<String, List<String>> requestHeader,
537537

538538
redirectedUrl = connectTask.getFinalRedirectedUrl();
539539
if (acceptPartial || onlyFromBeginning) {
540-
final long totalLength = FileDownloadUtils.findInstanceLengthForTrial(id, connection);
540+
final long totalLength = FileDownloadUtils.findInstanceLengthForTrial(connection);
541541

542542
// update model
543543
String fileName = null;

‎library/src/main/java/com/liulishuo/filedownloader/download/FetchDataTask.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public void run() throws IOException, IllegalAccessException, IllegalArgumentExc
8282

8383
if (paused) return;
8484

85-
final long contentLength = FileDownloadUtils.findContentLength(connectionIndex, connection);
85+
long contentLength = FileDownloadUtils.findContentLength(connectionIndex, connection);
86+
if (contentLength == TOTAL_VALUE_IN_CHUNKED_RESOURCE) {
87+
contentLength = FileDownloadUtils.findContentLengthFromContentRange(connection);
88+
}
8689
if (contentLength == 0) {
8790
throw new FileDownloadGiveUpRetryException(FileDownloadUtils.
8891
formatString(

‎library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,13 @@ public static boolean isAcceptRange(int responseCode, FileDownloadConnection con
555555
// because of we using one of two HEAD method to request or using range:0-0 to trial connection
556556
// only if connection api not support, so we test content-range first and then test
557557
// content-length.
558-
public static long findInstanceLengthForTrial(int id, FileDownloadConnection connection) {
558+
public static long findInstanceLengthForTrial(FileDownloadConnection connection) {
559559
long length = findInstanceLengthFromContentRange(connection);
560-
if (length < 0) length = findContentLength(id, connection);
561-
if (length < 0) length = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
560+
if (length < 0) {
561+
length = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
562+
FileDownloadLog.w(FileDownloadUtils.class, "don't get instance length from"
563+
+ "Content-Range header");
564+
}
562565
// the response of HEAD method is not very canonical sometimes(it depends on server
563566
// implementation)
564567
// so that it's uncertain the content-length is the same as the response of GET method if
@@ -571,13 +574,16 @@ public static long findInstanceLengthForTrial(int id, FileDownloadConnection con
571574
}
572575

573576
public static long findInstanceLengthFromContentRange(FileDownloadConnection connection) {
574-
return parseContentRangeFoInstanceLength(
575-
connection.getResponseHeaderField("Content-Range"));
577+
return parseContentRangeFoInstanceLength(getContentRangeHeader(connection));
578+
}
579+
580+
private static String getContentRangeHeader(FileDownloadConnection connection) {
581+
return connection.getResponseHeaderField("Content-Range");
576582
}
577583

578584
public static long findContentLength(final int id, FileDownloadConnection connection) {
579-
long contentLength = FileDownloadUtils
580-
.convertContentLengthString(connection.getResponseHeaderField("Content-Length"));
585+
long contentLength = convertContentLengthString(
586+
connection.getResponseHeaderField("Content-Length"));
581587
final String transferEncoding = connection.getResponseHeaderField("Transfer-Encoding");
582588

583589
if (contentLength < 0) {
@@ -609,6 +615,31 @@ public static long findContentLength(final int id, FileDownloadConnection connec
609615
return contentLength;
610616
}
611617

618+
public static long findContentLengthFromContentRange(FileDownloadConnection connection) {
619+
final String contentRange = getContentRangeHeader(connection);
620+
long contentLength = parseContentLengthFromContentRange(contentRange);
621+
if (contentLength < 0) contentLength = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
622+
return contentLength;
623+
}
624+
625+
public static long parseContentLengthFromContentRange(String contentRange) {
626+
if (contentRange == null || contentRange.length() == 0) return -1;
627+
final String pattern = "bytes (\\d+)-(\\d+)/\\d+";
628+
try {
629+
final Pattern r = Pattern.compile(pattern);
630+
final Matcher m = r.matcher(contentRange);
631+
if (m.find()) {
632+
final long rangeStart = Long.parseLong(m.group(1));
633+
final long rangeEnd = Long.parseLong(m.group(2));
634+
return rangeEnd - rangeStart + 1;
635+
}
636+
} catch (Exception e) {
637+
FileDownloadLog.e(FileDownloadUtils.class, e, "parse content length"
638+
+ " from content range error");
639+
}
640+
return -1;
641+
}
642+
612643
public static String findFilename(FileDownloadConnection connection, String url) {
613644
String filename = FileDownloadUtils.parseContentDisposition(connection.
614645
getResponseHeaderField("Content-Disposition"));

‎library/src/test/java/com/liulishuo/filedownloader/util/FileDownloadUtilsTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,29 @@ public void parseContentDisposition() {
3535
assertThat(filename).isEqualTo("genome.jpeg");
3636
}
3737

38+
@Test
39+
public void parseContentLengthFromContentRange_withNullContentRange() {
40+
long length = FileDownloadUtils.parseContentLengthFromContentRange(null);
41+
assertThat(length).isEqualTo(-1);
42+
}
43+
44+
@Test
45+
public void parseContentLengthFromContentRange_withEmptyContentRange() {
46+
long length = FileDownloadUtils.parseContentLengthFromContentRange("");
47+
assertThat(length).isEqualTo(-1);
48+
}
49+
50+
@Test
51+
public void parseContentLengthFromContentRange_withStartToEndRange() {
52+
long length = FileDownloadUtils
53+
.parseContentLengthFromContentRange("bytes 25086300-37629450/37629451");
54+
assertThat(length).isEqualTo(12543151);
55+
}
56+
57+
@Test
58+
public void parseContentLengthFromContentRange_withUnavailableContentRange() {
59+
long length = FileDownloadUtils.parseContentLengthFromContentRange("bytes 0-/37629451");
60+
assertThat(length).isEqualTo(-1);
61+
}
62+
3863
}

0 commit comments

Comments
 (0)
Failed to load comments.