Skip to content

Commit

Permalink
Merge pull request #30 from chenxiaolong/ranges
Browse files Browse the repository at this point in the history
UpdaterThread: Check for Content-Range instead of Accept-Ranges
  • Loading branch information
chenxiaolong authored Dec 22, 2023
2 parents 0e30ad0 + 983158a commit c242197
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/src/main/java/com/chiller3/custota/updater/UpdaterThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,21 @@ class UpdaterThread(

PartialFdInputStream(pfd, pf.offset, pf.size)
} else {
val range = "${pf.offset}-${pf.offset + pf.size - 1}"

val connection = openUrl(URL(uri.toString()))
connection.setRequestProperty("Range", "bytes=${pf.offset}-${pf.offset + pf.size - 1}")
connection.setRequestProperty("Range", "bytes=$range")
connection.connect()

if (connection.responseCode / 100 != 2) {
throw IOException("Got ${connection.responseCode} (${connection.responseMessage}) for $uri")
}

if (connection.getHeaderField("Accept-Ranges") != "bytes") {
throw IOException("Server does not support byte ranges")
val responseRange = connection.getHeaderField("Content-Range")
?: throw IOException("Server does not support byte ranges")

if (responseRange.split('/').firstOrNull() != "bytes $range") {
throw IOException("Response range ($responseRange) does not match request ($range)")
}

if (connection.contentLengthLong != pf.size) {
Expand Down

0 comments on commit c242197

Please sign in to comment.