Skip to content

Bug: HTTP Range header incorrectly parses suffix-byte-range requests #29

@windoze95

Description

@windoze95

Description

In app/services/media_server.py (~line 23), the build_range_response function parses range requests using a simple string split:

    range_spec = range_header.replace("bytes=", "").strip()
    parts = range_spec.split("-")

    start = int(parts[0]) if parts[0] else 0
    end = int(parts[1]) if len(parts) > 1 and parts[1] else file_size - 1

If a client requests the last 500 bytes using the standard suffix-byte-range format bytes=-500, parts evaluates to ["", "500"].
Because parts[0] is empty, start is erroneously set to 0, and end becomes 500.
This results in the server returning the first 501 bytes instead of the last 500 bytes, which breaks clients that attempt to seek to the end of a file or read trailing metadata.

Suggested Fix

Check if parts[0] is empty and parts[1] is present. If so, calculate the start offset from the end of the file:

if not parts[0] and len(parts) > 1 and parts[1]:
    start = max(0, file_size - int(parts[1]))
    end = file_size - 1

File Path

app/services/media_server.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood-first-issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions