Skip to content

How can my app bound a download when Content-Length is absent or wrong?

Enforce your application's maximum result size while reading the response stream, even when Content-Length is missing or smaller than the actual body. Cancel the transfer and clean partial output when the bound is exceeded. Keep the completed AISwapLab task distinct from this local delivery rejection.

PROTECT API ACCESS AND RESULTSAISwapLab team · Editorial methodPublished 2 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: Set a delivery policy; Count as bytes arrive; Test misleading envelopes.
Workflow diagram: Set a delivery policy → Count as bytes arrive → Test misleading envelopes. Read the article for evidence and limits.

Headers are only an early check

A declared length can help reject obviously oversized content, but it is not a complete bound on bytes your application processes. A streaming counter prevents unbounded memory or disk growth when the length is absent or an intermediary behaves unexpectedly. Locate decompression before choosing the counter: some clients expose an already decoded response stream. A small encoded Content-Length cannot bound decoded memory use. Count the bytes actually consumed after automatic decoding where applicable; document this layer rather than assuming every AISwapLab output uses compression.

What to do next

  1. 01

    Set a delivery policy

    Choose supported maximum sizes for each local processing path based on your application's resources and actual needs. Distinguish that local policy from provider upload limits or a guaranteed output size, which it does not establish.

  2. 02

    Count as bytes arrive

    Check a declared length early when available, then increment a counter for every chunk before retaining or writing it. Abort at the limit, release stream resources and remove incomplete files without marking them as verified assets.

  3. 03

    Test misleading envelopes

    Use fixture streams with no length, a too-small declared length and a body that exceeds the policy mid-transfer. Assert bounded resource use, partial-file cleanup and a recoverable delivery error referencing the original completed job.

A concrete example

Illustrative example: a test response omits Content-Length and keeps streaming. The application's counter reaches its configured bound, cancels the read and deletes the partial file. It reports a delivery policy error rather than launching another generation.

Before you call it ready

  • Limits apply during streaming.
  • Partial output never becomes a verified asset.
  • Cancellation releases readers and files.
  • An oversized compressed-expansion fixture proves the decoded consumer limit aborts and releases resources even when the encoded body is small.

When to stop or choose another method

Stop reading an unbounded response into memory merely because the server omitted Content-Length or claimed a small value.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation