Skip to main content

Module upload

Module upload 

Source
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/list with application/octet-stream chunk bodies and the required per-chunk X-Capsule-Checksum (lowercase-hex SHA-256), X-Capsule-Offset, and the X-Capsule-Protocol handshake 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, a HEAD, 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 (or HEAD), after its Retry-After; too_fragmented → let the ranges in flight finish, wait, re-plan; too_many_in_flight → wait, narrow P to the session’s advertised ranges; a lost answer (a reset, or no response) → HEAD before 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_mismatch at finalization → a typed UploadError::SourceChanged when the source changed, one re-created session when it affirms it is unchanged, else the refusal; rate_limited, unavailable, storage_full and every other answer the retry classification marks retryable (any 5xx, 408, 425) → wait out Retry-After (else the retry engine’s backoff), then resume from HEAD. The client switches on the stable error.* code (from capsule_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 a 408.

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§

AdaptiveChunkSizeStrategy
Chunk size strategy for adaptive upload.
CreateSessionResponse
The POST /upload success body.
CreateUploadRequest
The POST /upload request body. Plaintext metadata (filename, capture date) is deliberately absent — it rides the encrypted metadata blob.
HeadInfo
A HEAD /upload/{id} result: the resumption primitive.
ManifestEnvelope
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.
SessionSummary
A row from GET /upload/sessions — the subset the resuming client needs. Unknown server fields are tolerated (serde ignores them).
SourceChanged
The source of a blob no longer matches what its bundle was built from.
StaticToken
A fixed bearer token, for tests and callers that already hold a live token. Production clients compose with the SDK session via UploadTransport::with_session instead.
UploadClient
The hand-written chunked, resumable, adaptive upload client.
UploadTransport
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§

BlobRole
The blob’s role within its asset bundle (closed enum; snake_case on the wire to match the server).
CreateOutcome
How POST /upload resolved.
SourceIdentity
What UploadSource::check_unchanged can say about a source it found no change in.
UploadError
Upload client errors.
UploadOutcome
How a full UploadClient::upload resolved.

Constants§

ADVERSE_TARGET_REQUEST_DURATION
How long one chunk request should take under an ConnectionClass::Adverse link: 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§

TransferGate
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’s UploadQueue, so every range any session sends reserves its length against the one shared byte budget.
UploadSource
The bytes of one blob, read a chunk at a time.