Expand description
Upload client module for Capsule SDK.
The hand-written, chunked, resumable, adaptive upload client. The protocol is too stateful for codegen — the spargen-generated REST client covers the plain request/response surfaces instead — so this module owns the wire directly:
create/PATCH/HEAD/DELETE/listwithapplication/octet-streamchunk bodies and the required per-chunkX-Capsule-Checksum(lowercase-hex SHA-256),X-Capsule-Offset, and theX-Capsule-Protocolhandshake header;- the normative adaptive chunk-size algorithm (
AdaptiveChunkSizeStrategy), clamped to[PROTOCOL_MIN_CHUNK, PROTOCOL_MAX_CHUNK]with 4 KiB alignment guaranteed by construction; - the range scheduler (
UploadClient::with_ranges_window): up to P ranges of one session in flight, each planned from the server’s committed set (committed) by filling the lowest gap first, so a sequential transfer is the case P = 1. A session-level step (a wait, aHEAD, a re-create) is taken only once every range in flight has returned, so no wait ever stalls a request still sending; - the code-driven recovery matrix (
UploadClient::upload):range_conflict→ re-plan from the committed set it carries (orHEAD), after itsRetry-After;too_fragmented→ let the ranges in flight finish, wait, re-plan;too_many_in_flight→ wait, narrow P to the session’s advertisedranges; a lost answer (a reset, or no response) →HEADbefore anything is re-sent;offset_mismatch(an older server) → re-align;session_not_found→ re-create;duplicate_blob→ merge (success carrying the existing asset ref);426→ abort-with-upgrade;checksum_mismatch→ re-send the chunk;chunk_timeout→ re-send at the same offset, shorter;content_hash_mismatchat finalization → a typedUploadError::SourceChangedwhen the source changed, one re-created session when it affirms it is unchanged, else the refusal;rate_limited,unavailable,storage_fulland every other answer the retry classification marks retryable (any5xx,408,425) → wait outRetry-After(else the retry engine’s backoff), then resume from HEAD. The client switches on the stableerror.*code (fromcapsule_i18n::error_codes); the status decides only for an answer whose code names no action; - per-request stall bounds (
RequestBounds): a chunk body is cut when no bytes move for the stall bound, the wait for a response once the body is sent runs under a separate, longer bound, and nothing bounds a request’s total duration — a slow but live link is never cut. A client-cut chunk is re-sent at the same offset, shorter, like a408.
Tokens stay SDK-owned: an upload composes with the SDK session — build the
transport with UploadTransport::with_session and every request runs through
crate::auth::Session::execute (pre-flight refresh, single-flight, one 401
replay), so callers never juggle raw access tokens. StaticToken +
UploadTransport::with_static_token plug in a fixed bearer for tests and
callers that already hold a live token. No filename or plaintext metadata ever
rides the wire — it travels inside the encrypted metadata blob (transfer
upload doc, §Chunk Rules).
Modules§
- advice
- Server advice and a session’s relative expiry, read leniently.
- committed
- A session’s committed set and the range scheduler’s plan over it.
- queue
- The device-wide upload queue, keyed per server (transfer upload doc, §Queueing Across Servers).
Structs§
- Adaptive
Chunk Size Strategy - Chunk size strategy for adaptive upload.
- Create
Session Response - The
POST /uploadsuccess body. - Create
Upload Request - The
POST /uploadrequest body. Plaintext metadata (filename, capture date) is deliberately absent — it rides the encrypted metadata blob. - Head
Info - A
HEAD /upload/{id}result: the resumption primitive. - Manifest
Envelope - The server-visible mirror of the signed manifest’s envelope fields, declared
at
POST /upload(owned by the provenance design doc). Opaque to this client — the import pipeline builds and signs it; the SDK only carries it on the wire. - Session
Summary - A row from
GET /upload/sessions— the subset the resuming client needs. Unknown server fields are tolerated (serde ignores them). - Source
Changed - The source of a blob no longer matches what its bundle was built from.
- Static
Token - A fixed bearer token, for tests and callers that already hold a live token.
Production clients compose with the SDK session via
UploadTransport::with_sessioninstead. - Upload
Client - The hand-written chunked, resumable, adaptive upload client.
- Upload
Transport - The authorized HTTP transport for one upload endpoint: base URL, the pinned
protocol date sent as
X-Capsule-Protocol, the authorization seam, and the stall bounds every request runs under.
Enums§
- Blob
Role - The blob’s role within its asset bundle (closed enum; snake_case on the wire to match the server).
- Create
Outcome - How
POST /uploadresolved. - Source
Identity - What
UploadSource::check_unchangedcan say about a source it found no change in. - Upload
Error - Upload client errors.
- Upload
Outcome - How a full
UploadClient::uploadresolved.
Constants§
- ADVERSE_
TARGET_ REQUEST_ DURATION - How long one chunk request should take under an
ConnectionClass::Adverselink: two seconds, so a request usually completes between the resets that made the link adverse. - DEFAULT_
CLIENT_ RANGES - The ranges of one session a client holds in flight unless told otherwise: the lane’s default
P (
queue::DEFAULT_RANGES_WINDOW). - PROTOCOL_
MAX_ CHUNK - PROTOCOL_
MIN_ CHUNK - Protocol-surface chunk bounds (transfer upload doc, §Chunk Rules and Strictness). Every adaptive tier range sits inside these; the server rejects outside them (400 / 413).
- TARGET_
REQUEST_ DURATION - How long one chunk request should take at the measured goodput, on an ordinary link: eight seconds.
Traits§
- Transfer
Gate - The scheduler a gated transfer runs under (
UploadClient::upload_gated): it holds the memory each range in flight needs and hears the server’s advice. The push driver implements it over the device’sUploadQueue, so every range any session sends reserves its length against the one shared byte budget. - Upload
Source - The bytes of one blob, read a chunk at a time.