~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(library): restore PDF byte-range responses (#7291)
LearningCircuitb13bbd849d81
diff --git a/changelog.d/5760.bugfix.md b/changelog.d/5760.bugfix.mdnew file mode 100644index 0000000000..a913d84480--- /dev/null+++ b/changelog.d/5760.bugfix.md@@ -0,0 +1 @@+Restore single byte-range downloads for library PDFs so viewers and download clients can request portions of a document.diff --git a/src/local_deep_research/web/routers/library.py b/src/local_deep_research/web/routers/library.pyindex e58e62e57b..1d5fe783c2 100644--- a/src/local_deep_research/web/routers/library.py+++ b/src/local_deep_research/web/routers/library.py@@ -18,6 +18,7 @@ from ..dependencies.threadpool import (WorkerCleanupStreamingResponse,run_db_sync,)+from ..utils.range_response import in_memory_range_responsefrom ..template_config import templatesfrom ..utils.download_headers import build_content_disposition@@ -584,8 +585,9 @@ def _serve_pdf(# splits on every 0x0A, which in binary PDF data occurs roughly# every 256 bytes, so a single view became tens of thousands of# threadpool dispatches and sent no Content-Length.- return Response(+ return in_memory_range_response(content=pdf_bytes,+ request=request,media_type="application/pdf",headers={"Content-Disposition": build_content_disposition(diff --git a/src/local_deep_research/web/utils/range_response.py b/src/local_deep_research/web/utils/range_response.pynew file mode 100644index 0000000000..d49b5d7676--- /dev/null+++ b/src/local_deep_research/web/utils/range_response.py@@ -0,0 +1,69 @@+"""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 responsediff --git a/tests/security/test_client_side_xss_sinks.py b/tests/security/test_client_side_xss_sinks.pyindex d6d2338939..1a7245ed50 100644--- a/tests/security/test_client_side_xss_sinks.py+++ b/tests/security/test_client_side_xss_sinks.py@@ -1019,8 +1019,11 @@ def dynamic_media_types(text: str) -> list[str]:def test_media_type_scanner_flags_a_guessed_content_type():- unsafe = "return Response(body, media_type=guess_type(filename)[0])\n"- assert dynamic_media_types(unsafe) == ["guess_type(filename)[0]"]+ for constructor in ("Response", "in_memory_range_response"):+ unsafe = (+ f"return {constructor}(body, media_type=guess_type(filename)[0])\n"+ )+ assert dynamic_media_types(unsafe) == ["guess_type(filename)[0]"]def test_media_type_scanner_is_quiet_on_a_literal():@@ -1041,9 +1044,13 @@ def test_no_served_content_type_is_derived_from_a_filename():assert literals >= 5, (f"expected the known media_type= call sites, found {literals}")- # The single non-literal is the export router's format->mimetype- # mapping, which never sees a filename.- assert dynamic == {"web/routers/research.py": ["mimetype"]}, dynamic+ # The export router uses a closed format->mimetype mapping. The range+ # helper forwards its caller's media type; this same census checks those+ # callers, including the library route's literal application/pdf.+ assert dynamic == {+ "web/routers/research.py": ["mimetype"],+ "web/utils/range_response.py": ["media_type"],+ }, dynamicdef test_no_module_guesses_a_content_type_from_a_path():diff --git a/tests/web/routers/test_library_port_fidelity.py b/tests/web/routers/test_library_port_fidelity.pyindex a61880478a..b3d719068d 100644--- a/tests/web/routers/test_library_port_fidelity.py+++ b/tests/web/routers/test_library_port_fidelity.py@@ -2,8 +2,8 @@Three independent regressions were identified, all in``src/local_deep_research/web/routers/library.py`` (plus the shared-body-parsing idiom in ``library_search.py``). The remaining byte-range tests-are ``xfail(strict=True)``; fixed regressions are permanent passing tests.+body-parsing idiom in ``library_search.py``). These fixed regressions are+permanent passing tests.1. Byte-range / conditional PDF serving.main returned the PDF through Flask's ``send_file(BytesIO(pdf_bytes), ...)``.@@ -19,9 +19,8 @@ are ``xfail(strict=True)``; fixed regressions are permanent passing tests.>>> rv.status_code, rv.headers["Content-Range"](206, 'bytes 0-99/2016')- The port replaced that with a bare ``Response(content=pdf_bytes, ...)``,- which has no range handling at all: no ``Accept-Ranges`` header, and a- ranged request gets the whole blob back as a 200.+ The port initially replaced that with a bare ``Response(content=pdf_bytes,+ ...)``. The in-memory response now handles single byte ranges again.2. ``text/html`` on the two browser-navigation PDF/text page routes.``pages/library.html`` links ``/library/document/<id>/pdf`` and@@ -164,38 +163,24 @@ def test_pdf_route_serves_the_stored_blob(library_client):assert resp.content == PDF_BYTES- strict=True,- reason=(- "library.py view_pdf_page returns a bare fastapi Response instead of "- "main's send_file(BytesIO(...), conditional=True); a bare Response "- "never emits Accept-Ranges, so clients are told the PDF is not "- "seekable."- ),-)-def test_pdf_route_advertises_accept_ranges(library_client):[email protected]("prefix", ["/library", "/library/api"])+def test_pdf_route_advertises_accept_ranges(library_client, prefix):client, with_pdf, _ = library_client- resp = client.get(f"/library/document/{with_pdf}/pdf")+ resp = client.get(f"{prefix}/document/{with_pdf}/pdf")+ assert resp.status_code == 200+ assert resp.content == PDF_BYTESassert resp.headers.get("accept-ranges") == "bytes", ("main's send_file set 'Accept-Ranges: bytes' on this response; got "f"headers {dict(resp.headers)}")- strict=True,- reason=(- "library.py view_pdf_page returns a bare fastapi Response, which "- "ignores the Range request header entirely and replies 200 with the "- "whole blob; main's send_file(conditional=True) replied 206 with a "- "Content-Range and only the requested slice."- ),-)-def test_pdf_route_honours_a_range_request(library_client):[email protected]("prefix", ["/library", "/library/api"])+def test_pdf_route_honours_a_range_request(library_client, prefix):client, with_pdf, _ = library_clientresp = client.get(- f"/library/document/{with_pdf}/pdf", headers={"Range": "bytes=0-99"}+ f"{prefix}/document/{with_pdf}/pdf", headers={"Range": "bytes=0-99"})assert resp.status_code == 206, (@@ -204,6 +189,93 @@ def test_pdf_route_honours_a_range_request(library_client):)assert resp.headers.get("content-range") == f"bytes 0-99/{len(PDF_BYTES)}"assert resp.content == PDF_BYTES[:100]+ assert resp.headers["content-length"] == "100"+ assert resp.headers["content-type"] == "application/pdf"+ assert resp.headers["content-disposition"].startswith("inline;")++[email protected]("prefix", ["/library", "/library/api"])+ ("value", "start", "stop"),+ [+ ("bytes=-10", len(PDF_BYTES) - 10, len(PDF_BYTES)),+ ("bytes=2000-", 2000, len(PDF_BYTES)),+ ("bytes=2000-999999", 2000, len(PDF_BYTES)),+ ("bytes=-999999", 0, len(PDF_BYTES)),+ ],+)+def test_pdf_route_clips_ranges(library_client, prefix, value, start, stop):+ client, with_pdf, _ = library_client+ resp = client.get(+ f"{prefix}/document/{with_pdf}/pdf", headers={"Range": value}+ )++ assert resp.status_code == 206+ assert resp.content == PDF_BYTES[start:stop]+ assert resp.headers["content-length"] == str(stop - start)+ assert resp.headers["content-range"] == (+ f"bytes {start}-{stop - 1}/{len(PDF_BYTES)}"+ )++[email protected]("prefix", ["/library", "/library/api"])+def test_pdf_route_unsatisfiable_range_is_empty(library_client, prefix):+ client, with_pdf, _ = library_client+ resp = client.get(+ f"{prefix}/document/{with_pdf}/pdf",+ headers={"Range": f"bytes={len(PDF_BYTES)}-"},+ )++ assert resp.status_code == 416+ assert resp.content == b""+ assert resp.headers["content-range"] == f"bytes */{len(PDF_BYTES)}"+ assert resp.headers["content-length"] == "0"++[email protected]("prefix", ["/library", "/library/api"])+ "headers",+ [+ {"Range": "bytes=invalid"},+ {"Range": "bytes=0-99,200-299"},+ {"Range": "bytes=0-99", "If-Range": '"older-document"'},+ ],+)+def test_pdf_route_ignored_range_keeps_complete_body(+ library_client, prefix, headers+):+ client, with_pdf, _ = library_client+ resp = client.get(f"{prefix}/document/{with_pdf}/pdf", headers=headers)++ assert resp.status_code == 200+ assert resp.content == PDF_BYTES+ assert resp.headers["content-length"] == str(len(PDF_BYTES))+ assert "content-range" not in resp.headers++[email protected]("prefix", ["/library", "/library/api"])+def test_pdf_range_requires_authentication(library_client, prefix, monkeypatch):+ from local_deep_research.web.routers.library import PDFStorageManager++ client, with_pdf, _ = library_client++ def unexpected_load(*args, **kwargs):+ pytest.fail("An unauthenticated range request must not load a PDF")++ monkeypatch.setattr(PDFStorageManager, "load_pdf", unexpected_load)+ anonymous = TestClient(client.app, raise_server_exceptions=False)+ try:+ resp = anonymous.get(+ f"{prefix}/document/{with_pdf}/pdf",+ headers={"Range": "bytes=0-99"},+ follow_redirects=False,+ )+ finally:+ anonymous.close()++ assert resp.status_code in {302, 401}+ assert "content-range" not in resp.headers+ assert not resp.content.startswith(b"%PDF")# ---------------------------------------------------------------------------diff --git a/tests/web/routers/test_streaming_contracts.py b/tests/web/routers/test_streaming_contracts.pyindex 7dc576a860..b9de3c4081 100644--- a/tests/web/routers/test_streaming_contracts.py+++ b/tests/web/routers/test_streaming_contracts.py@@ -435,9 +435,11 @@ def test_binary_export_routes_use_response_not_streamingresponse():find_streamingresponse_bytesio,)+ range_response_path = ROUTERS_DIR.parent / "utils" / "range_response.py"binary_export_files = [ROUTERS_DIR / "library.py", # PDF serving (view_pdf_page)ROUTERS_DIR / "research.py", # report export (export_research_report)+ range_response_path, # buffered byte-range helper used by the PDF route]for path in binary_export_files:tree = _parse(path)@@ -449,14 +451,17 @@ def test_binary_export_routes_use_response_not_streamingresponse():# And the positive half: each of those files actually DOES serve its# binary payload via a real, non-streaming media_type on a plain- # Response(...) call -- proving the assertion above isn't vacuously- # true because neither file has a binary export at all.+ # Response(...) call or its range helper -- proving the assertion above+ # isn't vacuously true because a file has no binary export at all.real_media_types_by_file = {}for path in binary_export_files:tree = _parse(path)found = []for node in ast.walk(tree):- if _call_target_name(node) != "Response":+ if _call_target_name(node) not in {+ "Response",+ "in_memory_range_response",+ }:continuefor kw in node.keywords:if kw.arg == "media_type":@@ -473,6 +478,14 @@ def test_binary_export_routes_use_response_not_streamingresponse():for value in pdf_media_types:assert value not in ALLOWED_STREAMING_MEDIA_TYPES, value+ # Following the helper must not hide a new StreamingResponse or an+ # internally replaced media type. Its caller supplies the PDF literal.+ assert find_streamingresponse_media_types(_parse(range_response_path)) == []+ helper_media_types = real_media_types_by_file["range_response.py"]+ assert len(helper_media_types) == 1, helper_media_types+ assert isinstance(helper_media_types[0], ast.Name)+ assert helper_media_types[0].id == "media_type"+# research.py's export route: media_type is the exporter-resolved# `mimetype` variable (PDF/ODT/RIS/... vary per format), not a# hardcoded streaming-contract literal.diff --git a/tests/web/utils/test_range_response.py b/tests/web/utils/test_range_response.pynew file mode 100644index 0000000000..6edec4de8a--- /dev/null+++ b/tests/web/utils/test_range_response.py@@ -0,0 +1,150 @@+"""Partial in-memory downloads preserve HTTP range and response contracts."""++import pytest+import sys+from starlette.requests import Request++from local_deep_research.web.utils.range_response import (+ in_memory_range_response,+)++CONTENT = b"0123456789"+++def _response(headers=(), *, method="GET", content=CONTENT):+ request = Request(+ {+ "type": "http",+ "method": method,+ "headers": [+ (key.lower().encode("ascii"), value.encode("ascii"))+ for key, value in headers+ ],+ }+ )+ return in_memory_range_response(+ content,+ request=request,+ media_type="application/pdf",+ headers={"Content-Disposition": 'inline; filename="report.pdf"'},+ )+++ ("value", "body", "content_range"),+ [+ ("bytes=0-3", b"0123", "bytes 0-3/10"),+ ("bytes=2-5", b"2345", "bytes 2-5/10"),+ ("bytes=9-9", b"9", "bytes 9-9/10"),+ ("bytes=8-", b"89", "bytes 8-9/10"),+ ("bytes=0-", CONTENT, "bytes 0-9/10"),+ ("bytes=-3", b"789", "bytes 7-9/10"),+ ("bytes=-10", CONTENT, "bytes 0-9/10"),+ ("bytes=-99", CONTENT, "bytes 0-9/10"),+ ("bytes=8-99", b"89", "bytes 8-9/10"),+ ("bytes=00-03", b"0123", "bytes 0-3/10"),+ ("BYTES=0-3", b"0123", "bytes 0-3/10"),+ ("bytes= 0-3 ", b"0123", "bytes 0-3/10"),+ ("bytes=-" + "9" * 100, CONTENT, "bytes 0-9/10"),+ ("bytes=8-" + "9" * 100, b"89", "bytes 8-9/10"),+ ],+)+def test_single_range(value, body, content_range):+ response = _response([("Range", value)])++ assert response.status_code == 206+ assert response.body == body+ assert response.headers["content-range"] == content_range+ assert response.headers["content-length"] == str(len(body))+ assert response.headers["accept-ranges"] == "bytes"+ assert response.headers["content-type"] == "application/pdf"+ assert (+ response.headers["content-disposition"]+ == 'inline; filename="report.pdf"'+ )+++ "value", ["bytes=10-", "bytes=99-100", "bytes=" + "9" * 100 + "-"]+)+def test_unsatisfiable_range(value):+ response = _response([("Range", value)])++ assert response.status_code == 416+ assert response.body == b""+ assert response.headers["content-range"] == "bytes */10"+ assert response.headers["content-length"] == "0"+ assert response.headers["accept-ranges"] == "bytes"+++ "headers",+ [+ [],+ [("Range", "")],+ [("Range", "items=0-3")],+ [("Range", "bytes=wrong")],+ [("Range", "bytes=-")],+ [("Range", "bytes=0--1")],+ [("Range", "bytes=1_0-99")],+ [("Range", "bytes=0-3;extra")],+ [("Range", "bytes=0-3\n")],+ [("Range", "bytes=0-3\x00")],+ [("Range", "bytes=5-2")],+ [("Range", "bytes=-0")],+ [("Range", "bytes=-000")],+ [("Range", "bytes=+1-3")],+ [("Range", "bytes=1.0-3")],+ [("Range", "bytes=0-1,4-5")],+ [("Range", "bytes=0-3,2-5")],+ [("Range", "bytes=0-3,")],+ [("Range", "bytes=0-3"), ("Range", "bytes=5-6")],+ [("Range", "bytes=0-3"), ("If-Range", '"older-document"')],+ [("Range", "bytes=0-3"), ("If-Range", "Wed, 21 Oct 2015 07:28:00 GMT")],+ [("Range", "bytes=0-3"), ("If-Range", "")],+ pytest.param(+ [("Range", "bytes=" + "9" * 5000 + "-")],+ id="oversized-integer",+ ),+ ],+)+def test_ignored_ranges_return_the_complete_response(headers):+ response = _response(headers)++ assert response.status_code == 200+ assert response.body == CONTENT+ assert response.headers["content-length"] == "10"+ assert response.headers["accept-ranges"] == "bytes"+ assert "content-range" not in response.headers++[email protected]("method", ["HEAD", "POST", "OPTIONS"])+def test_range_only_applies_to_get(method):+ response = _response([("Range", "bytes=0-3")], method=method)++ assert response.status_code == 200+ assert response.headers["content-length"] == "10"+ assert "content-range" not in response.headers+++def test_empty_representation_has_no_satisfiable_range():+ response = _response([("Range", "bytes=0-")], content=b"")++ assert response.status_code == 416+ assert response.body == b""+ assert response.headers["content-range"] == "bytes */0"+ assert response.headers["content-length"] == "0"+++def test_configured_integer_limit_keeps_the_complete_response():+ original_limit = sys.get_int_max_str_digits()+ try:+ sys.set_int_max_str_digits(640)+ response = _response([("Range", "bytes=" + "9" * 700 + "-")])+ finally:+ sys.set_int_max_str_digits(original_limit)++ assert response.status_code == 200+ assert response.body == CONTENT+ assert response.headers["content-length"] == "10"+ assert "content-range" not in response.headers