KatInterview

Handling Seekable Body Replays on 307 Redirects in Python requests

Data Structure DesignModel answer
MediumModel answerData Structure Designpythonhttpdebugging

Last updated

Debug and implement stateful body rewind logic for seekable file-like streams when executing HTTP 307 redirects in an HTTP client library.

You are maintaining a Python HTTP client library similar to requests. When executing a POST request that receives an HTTP 307 Temporary Redirect (or 308 Permanent Redirect), the HTTP standard dictates that the client must re-issue the POST request to the target URL using the exact same request body.

Currently, string payload bodies work seamlessly across redirects. However, passing seekable byte streams (such as io.BytesIO) causes the second request attempt to hang and eventually hit a read timeout, or send 0 bytes.

Problem Scenario

Consider three test scenarios:

  1. String Body: Passing data='test'. The initial POST sends test. Upon a 307 redirect, the second POST also sends test. (Currently PASSING)
  2. Full Seekable Stream: Passing data=io.BytesIO(b'test'). The stream position starts at 0. (Currently FAILING / TIMING OUT)
  3. Partially Read Seekable Stream: Passing data=io.BytesIO(b'hello world') after reading 5 bytes (stream cursor is at position 5). Both the initial request and the redirected request must send only world (the payload starting from the initial cursor position). (Currently FAILING / TIMING OUT)
Payload (data)Initial Stream CursorTarget Body Sent on Attempt 1Target Body Sent on Attempt 2 (Post-307)
'test'N/A'test''test'
io.BytesIO(b'test')0b'test'b'test'
io.BytesIO(b'hello world') (after read(5))5b' world'b' world'

Below are the test cases that demonstrate the expected behavior:

def test_307_redirect_with_string(httpbin):
    r = requests.post(
        httpbin('redirect-to'), 
        data='test', 
        params={'url': httpbin('post'), 'status_code': 307}, 
        timeout=5
    )
    assert r.status_code == 200
    assert r.history[0].status_code == 307
    assert r.history[0].is_redirect
    assert r.json()['data'] == 'test'

def test_307_redirect_with_seekable_stream(httpbin):
    byte_str = b'test'
    r = requests.post(
        httpbin('redirect-to'), 
        data=io.BytesIO(byte_str), 
        params={'url': httpbin('post'), 'status_code': 307}, 
        timeout=5
    )
    assert r.status_code == 200
    assert r.history[0].status_code == 307
    assert r.history[0].is_redirect
    assert r.json()['data'] == byte_str.decode('utf-8')

def test_307_redirect_with_partially_read_stream(httpbin):
    data = io.BytesIO(b'hello world')
    data.read(5)  # Advance position to 5
    r = requests.post(
        httpbin('redirect-to'), 
        data=data, 
        params={'url': httpbin('post'), 'status_code': 307}, 
        timeout=5
    )
    assert r.status_code == 200
    assert r.history[0].status_code == 307
    assert r.history[0].is_redirect
    assert r.json()['data'] == ' world'

Task

  1. Explain the root cause of why plain strings succeed while seekable streams hang or fail upon 307 redirects.
  2. Outline the exact modifications to request preparation and redirect processing required to fix the stream replay mechanism.
  3. Account for streams that were already offset prior to the initial request call, as well as non-seekable streams (e.g., sockets, generators).

Hints

Hint 1

Think about what happens to an `io.BytesIO` stream's internal read pointer when read during the first HTTP POST transmission. What state is it in when the client attempts the redirected POST?

Hint 2

If you reset the stream pointer using `.seek(0)`, what breaks in the partially read stream case? How can you preserve the original stream start offset?

Hint 3

How should the HTTP client detect if a stream supports rewinding vs. when it is unseekable (like a socket or generator)?