Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.7 KB
Raw Blame
"""Byte-range responses for content already loaded into memory."""

from collections.abc import Mapping
import re

from starlette.requests import Request
from starlette.responses import Response


def in_memory_range_response(
    content: bytes,
    *,
    request: Request,
    media_type: str,
    headers: Mapping[str, str] | None = None,
) -> Response:
    """Honor one GET byte range without writing the content to a file.

    Unsupported or malformed ranges use the complete response. These downloads
    have no ETag or Last-Modified validator, so If-Range also selects the full
    response instead of risking a slice from a changed document.
    """
    response = Response(content, media_type=media_type, headers=headers)
    response.headers["Accept-Ranges"] = "bytes"
    if request.method != "GET" or "if-range" in request.headers:
        return response

    ranges = request.headers.getlist("range")
    # Do not partially honor a multi-range request or generate multipart data.
    # Ignore oversized numeric input before the parser converts it to integers.
    if len(ranges) != 1 or len(ranges[0]) > 1024 or "," in ranges[0]:
        return response
    unit, separator, value = ranges[0].partition("=")
    if not separator or unit.strip(" \t").lower() != "bytes":
        return response
    match = re.fullmatch(r"([0-9]*)[ \t]*-[ \t]*([0-9]*)", value.strip(" \t"))
    if match is None or not any(match.groups()):
        return response

    length = len(content)
    first, last = match.groups()
    try:
        if not first:
            suffix_length = int(last)
            if suffix_length == 0:
                return response
            start, stop = max(0, length - suffix_length), length
        else:
            start = int(first)
            stop = int(last) + 1 if last else length
            if last and stop <= start:
                return response
    except ValueError:
        # Respect an interpreter's configured integer-conversion limit too.
        return response

    if start >= length:
        response.status_code = 416
        response.body = b""
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: HTTP Content-Range uses integer byte offsets and never reaches SQL
        response.headers["Content-Range"] = f"bytes */{length}"
    else:
        stop = min(stop, length)
        response.status_code = 206
        response.body = content[start:stop]
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: HTTP Content-Range uses integer byte offsets and never reaches SQL
        response.headers["Content-Range"] = f"bytes {start}-{stop - 1}/{length}"
    response.headers["Content-Length"] = str(len(response.body))
    return response