bugger.
All challenges/Pythonhardthe-upload-that-cannot-replay-v1
THE INVESTIGATION

The upload that cannot replay

A redirected upload reuses its advertised length after its stream has already been consumed.

Requests & BoundariesStreamsReplayResource lifecycle

What’s going wrong

A POST upload succeeds once but sends an empty payload after a body-preserving redirect. Its advertised length still describes the original body. Fix the stream lifecycle without replaying discarded bodies.

Stdin JSON has body (ASCII text), start (integer offset from 0 through its length), seekable (boolean), and redirects (at most 20 status codes chosen from 301,302,303,307,308). One POST is sent first using the suffix beginning at start. Reading consumes the stream. Every listed status triggers another send; final success is implicit. 301/302/303 change the request to GET and permanently remove its body and length. 307/308 preserve the current method/body. For each preserved redirect with a nonempty body, restore the recorded initial cursor before sending. A nonseekable stream cannot be restored: stop before the next send with error unrewindable. A zero-length body needs no rewind.

Print {"sent":[...],"error":null or "unrewindable"}. Each sent record has method, length (advertised byte count), and body (actual text sent). Retain history on failure. ASCII makes characters equal bytes in this later exercise; focus on cursor ownership, not encoding.

Expected behavior

Maintain framing invariants across consumed resources, replay failures and method changes.

Reproduction

01 Preserved POST must replay its body
{"body":"data","start":0,"seekable":true,"redirects":[307]}
{"sent":[{"method":"POST","length":4,"body":"data"},{"method":"POST","length":4,"body":"data"}],"error":null}
02 No redirect control
{"body":"data","start":0,"seekable":false,"redirects":[]}
{"sent":[{"method":"POST","length":4,"body":"data"}],"error":null}
03 Changing to GET drops rather than rewinds body
{"body":"data","start":0,"seekable":false,"redirects":[303]}
{"sent":[{"method":"POST","length":4,"body":"data"},{"method":"GET","length":0,"body":""}],"error":null}

Before you begin

  • Build on byte lengths and redirect state: immutable request metadata must stay consistent with mutable stream position.
  • Python strings, lists and dictionaries
  • Read the report contract before changing code

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.