cashews icon indicating copy to clipboard operation
cashews copied to clipboard

Decoding of streaming response fails if the response contains headers with colons.

Open natuspati opened this issue 1 year ago • 1 comments

Description of the Issue

When decoding a streaming response, the presence of colons (:) in the headers causes the process to fail. This results in an error, making it impossible to handle certain responses with specific header formats.

Steps to Reproduce

Use the following code to simulate a streaming response with colons in the headers:

import uvicorn
from cashews import cache
from fastapi import FastAPI
from starlette.responses import StreamingResponse


app = FastAPI()
cache.setup("mem://?size=500")

_DATA = "data," * 10


async def _stream_data():
    print("Fetching data...")
    for data in _DATA.split():
        yield data


@app.get("/stream")
@cache(ttl="1m")
async def stream():
    headers = {"Date": "Tue, 14 Jan 2025 14:33:02 GMT", "Max-Forwards": "10"}
    return StreamingResponse(
        content=_stream_data(),
        headers=headers,
    )


if __name__ == "__main__":
    uvicorn.run(app, host="localhost")

The headers in the response contain colons, which triggers the error.

Expected Behavior

The streaming response is cached on the first request. On the following requests, the response is retrieved from cache and decoded correctly, even if colons are present in the headers.

Actual Behavior

The response is saved to the cache, but the decoding process fails, resulting in the following error:

server_error server_error_2

Environment

Python Version: 3.10
Framework Version: fastapi 0.115.5 

Proposed Solution

When decoding the raw value and splitting status_code from headers, use only a single split.

cashews/contrib/_starlette.py

async def decode_streaming_response(value: bytes, backend: Backend, key: str, **kwargs) -> StreamingResponse:
    if not await backend.get(f"{key}:done"):
        raise DecodeError()
    status_code, headers = value.split(b":", maxsplit=1)
    ...

natuspati avatar Jan 14 '25 15:01 natuspati

Thanks for reporting, I was on a long "vacation" , Sorry for taking so long to reply.

Is there any chance that you will reopen yours PR ?

Krukov avatar Mar 09 '25 18:03 Krukov

Fixed 7.4.1

Krukov avatar Jul 14 '25 22:07 Krukov