archivebox.misc.serve_static
Module Contents
Classes
Expose a write-only file-like object so zipfile can stream into a queue. |
|
Wraps a file like object with an iterator that runs over part (or all) of the file defined by start and stop. Blocks of block_size will be returned from the starting position, up to, but not including the stop point. https://github.com/satchamo/django/commit/2ce75c5c4bee2a858c0214d136bfcd351fcde11d |
Functions
Repair missing responsive images and equivalent recorded X API URLs. |
|
Resolve links before reading outputs, including during an active capture. |
|
Yield non-hidden files in a stable order so ZIP output is deterministic. |
|
Overrides Django’s built-in django.views.static.serve function to support byte range requests. This allows you to do things like seek into the middle of a huge mp4 or WACZ without downloading the whole file. https://github.com/satchamo/django/commit/2ce75c5c4bee2a858c0214d136bfcd351fcde11d |
|
Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders. |
|
Parses a range header into a list of two-tuples (start, stop) where |
Data
API
- archivebox.misc.serve_static.FAVICON_CACHE_CONTROL[source]
‘public, max-age=31536000, s-maxage=31536000, immutable’
- archivebox.misc.serve_static.add_replay_archive_fallbacks(response: django.http.HttpResponse) django.http.HttpResponse[source]
Repair missing responsive images and equivalent recorded X API URLs.
X’s client uses api.x.com/graphql without its original CSRF cookie, while authenticated capture uses x.com/i/api/graphql. Repair only GETs for that exact endpoint alias (also twitter.com), with an identical indexed query in the same local archive. Never reuse credentials or fetch a live API.
WHY: a recorder saves the image Chrome selected, not every srcset candidate. Replaying at another viewport/DPR can select an uncaptured URL (the motivating case was Drive’s missing 2x logo with its 1x logo already in the same WACZ). WACZ requests are answered inside Webrecorder’s service worker, so a Django 404 handler cannot repair them. Append this adapter when serving that worker; do not change captures, request more live assets, or resize the recorded page.
For images, only a GET that already returned 404 is eligible. The worker reads the referring HTML from the SAME archive and tries src/srcset candidates declared on that img or its enclosing picture. It never guesses filename substitutions or borrows an unrelated nearby image. The original response wins on absent metadata, unsupported worker APIs, parse errors, timeout, or failed candidates. This deliberately does not promise to fix JS-only srcsets, CSS backgrounds, corrupt images returning 200, or pages whose archived HTML is too large.
Keep this self-contained in the static-file server: it is a replay repair, independent of capture execution. Feature detection makes upstream worker changes disable the repair rather than break ordinary replay. The archive’s HTML/ZIP bytes and successful HTTP responses remain untouched.
- archivebox.misc.serve_static._load_hash_map(snapshot_dir: pathlib.Path) dict[str, str] | None[source]
- archivebox.misc.serve_static._read_hash_map(hashes_path: pathlib.Path, mtime_ns: int) dict[str, str][source]
- archivebox.misc.serve_static._hash_for_path(document_root: pathlib.Path, rel_path: str) str | None[source]
- archivebox.misc.serve_static._resolve_contained_path(root: pathlib.Path, path: pathlib.Path) pathlib.Path[source]
Resolve links before reading outputs, including during an active capture.
- archivebox.misc.serve_static._resolve_archive_path(document_root: str | pathlib.Path, rel_path: str) tuple[pathlib.Path, str][source]
- class archivebox.misc.serve_static._StreamingQueueWriter(output_queue: queue.Queue[bytes | BaseException | object])[source]
Expose a write-only file-like object so zipfile can stream into a queue.
Initialization
- archivebox.misc.serve_static._iter_visible_files(root: pathlib.Path)[source]
Yield non-hidden files in a stable order so ZIP output is deterministic.
- archivebox.misc.serve_static._build_directory_zip_response(fullpath: pathlib.Path, path: str, *, is_archive_replay: bool, use_async_stream: bool, config=None) django.http.StreamingHttpResponse[source]
- async archivebox.misc.serve_static._stream_ranged_file_async(ranged_file: RangedFileReader)[source]
- archivebox.misc.serve_static._render_directory_index(request, path: str, fullpath: pathlib.Path, document_root: pathlib.Path) django.http.HttpResponse[source]
- archivebox.misc.serve_static._render_image_preview_document(image_url: str, title: str) str[source]
- archivebox.misc.serve_static._encoded_responses_image_url(image_url: str, page_url: str | None) str | None[source]
- archivebox.misc.serve_static._index_responses_paths_for_html_images(snapshot_root: pathlib.Path, html_rel_path: str, encoded_urls: set[str], records: list[dict] | None = None) dict[str, str][source]
- archivebox.misc.serve_static._rewrite_html_image_sources_to_responses(html_text: str, snapshot_root: pathlib.Path, html_rel_path: str, page_url: str | None, replay_url: str | None = None, source_html: str | None = None) tuple[str, int][source]
- archivebox.misc.serve_static._rewrite_css_sources_to_responses(css_text: str, snapshot_root: pathlib.Path, rel_path: str) tuple[str, int][source]
- archivebox.misc.serve_static._rewrite_html_image_sources_for_request(request, html_text: str, document_root: pathlib.Path | None, html_rel_path: str, source_html: str | None = None) tuple[str, int][source]
- archivebox.misc.serve_static._set_transformed_response_headers(response, fullpath: pathlib.Path, statobj: os.stat_result, encoding: str | None, cache_policy: str) None[source]
- archivebox.misc.serve_static._is_risky_replay_document(fullpath: pathlib.Path, content_type: str) bool[source]
- archivebox.misc.serve_static._apply_archive_replay_headers(response: django.http.HttpResponse, *, fullpath: pathlib.Path, content_type: str, is_archive_replay: bool, config=None, **config_kwargs) django.http.HttpResponse[source]
- archivebox.misc.serve_static.serve_static_with_byterange_support(request, path, document_root=None, show_indexes=False, is_archive_replay: bool = False)[source]
Overrides Django’s built-in django.views.static.serve function to support byte range requests. This allows you to do things like seek into the middle of a huge mp4 or WACZ without downloading the whole file. https://github.com/satchamo/django/commit/2ce75c5c4bee2a858c0214d136bfcd351fcde11d
- archivebox.misc.serve_static.serve_static(request, path, **kwargs)[source]
Serve static files below a given point in the directory structure or from locations inferred from the staticfiles finders.
To use, put a URL pattern such as::
from django.contrib.staticfiles import views path('<path:path>', views.serve)in your URLconf.
It uses the django.views.static.serve() view to serve the found files.
- archivebox.misc.serve_static.parse_range_header(header, resource_size)[source]
Parses a range header into a list of two-tuples (start, stop) where
startis the starting byte of the range (inclusive) andstopis the ending byte position of the range (exclusive). Returns None if the value of the header is not syntactically valid. https://github.com/satchamo/django/commit/2ce75c5c4bee2a858c0214d136bfcd351fcde11d
- class archivebox.misc.serve_static.RangedFileReader(file_like, start=0, stop=float('inf'), block_size=None)[source]
Wraps a file like object with an iterator that runs over part (or all) of the file defined by start and stop. Blocks of block_size will be returned from the starting position, up to, but not including the stop point. https://github.com/satchamo/django/commit/2ce75c5c4bee2a858c0214d136bfcd351fcde11d
Initialization