Skip to content

Code Lab

Ghost Downloader Explained: How an HTTP Resume Checks Byte Ranges

By Miles Park·
Ghost Downloader repository preview

CODE LAB · PROJECT SPOTLIGHT

Ghost Downloader Python · GPL-3.0

Why the starting byte matters.

Project preview · XiaoYouChR/Ghost-Downloader-3

A download that stops halfway through a large file poses a simple question. Can the client trust the bytes the server sends next? Ghost Downloader supports several transfer types. Its HTTP task implementation contains a small range check that explains a crucial decision in a resumed transfer.

On November 24, 2026, GitHub Trending showed 1,078 stars gained today. GitHub Trending

Why the starting byte matters

Ghost Downloader project screenshot showing the Android task list
Ghost Downloader project screenshot showing the Android task list. Project source

The project’s Android task screen shows how transfers appear in the queue. Behind a single progress indicator, the HTTP worker has to associate received bytes with their intended positions. A wrong starting offset can produce a file of plausible size with the wrong contents.

Inside task.py

XiaoYouChR/Ghost-Downloader-3Python · source excerpt
PY  features/http_pack/task.py
def matchRange(contentRange: bytes, start: int, end: int, total: int | None) -> bool:    """RFC 9110 §14.4"""    unit, _, spec = contentRange.strip().partition(b" ")    span, _, size = spec.partition(b"/")    first, _, last = span.partition(b"-")    if unit.lower() != b"bytes" or not (first.isdigit() and last.isdigit() and size.isdigit()):        return False    return int(first) == start and start <= int(last) <= end and total in {None, int(size)}
Lines 37–44 · 83aaefbView original on GitHub ↗

The function splits the Content-Range value into its unit, first byte, last byte and total size. It accepts a byte range only when the fields are numeric, the starting position matches exactly and the end lies within the requested interval. When the caller already knows the total size, the reported value must agree. A shorter valid range can therefore pass this specific check.

Source credit · XiaoYouChR/Ghost-Downloader-3 contributors. GPL-3.0 license and notices.

Read a response header like the worker

For a requested interval of 100–199 in a 1,000-byte file, bytes 100-149/1000 passes the predicate. Changing the first byte to 99 fails even if the response contains useful-looking data. Changing the reported total to 1200 fails when the expected total is 1000. These examples isolate the header check. Throughput and the contents of the bytes require separate checks.

For an end-to-end check, use a file you control with a published checksum. Pause and resume the transfer, then compare the final checksum with the original. That catches a changed file of identical length, something a range header alone cannot detect.

Call the range checker before writing the next segment

Ghost Downloader’s Android notification example shows transfer activity outside the app
Ghost Downloader’s Android notification example shows transfer activity outside the app. The validation below happens beneath that progress display. View the project original

Suppose the first 100 bytes of a file are already on disk. The next worker asks for bytes 100 through 199, inclusive. Copy the pinned matchRange function above into a Python 3.10+ file, then add this driver beneath it. We ran the predicate from that revision against all five cases. No network connection is needed for this test.

Python · worked example

# Run below the matchRange definition from the source panel.
for header in (b"bytes 100-149/1000", b"bytes 99-149/1000",
               b"bytes 100-199/1200", b"bytes 100-200/1000"):
    print(header.decode(), matchRange(header, 100, 199, 1000))
print("Unknown expected size:",
      matchRange(b"bytes 100-149/1200", 100, 199, None))

Output from our run

bytes 100-149/1000 True
bytes 99-149/1000 False
bytes 100-199/1200 False
bytes 100-200/1000 False
Unknown expected size: True

The first reply can supply 50 bytes, covering offsets 100 through 149. A caller that consumes those bytes still needs 150 through 199. The fourth reply fails because byte 200 belongs outside the requested interval. In the last call, None means that the caller has no prior total to compare against, so 1200 passes that part of the check. It still takes an actual write, a completed transfer and an independent checksum to establish that the assembled file is intact.

More source walkthroughs in Code Lab

Join the discussion

Your email address will not be published. Required fields are marked *