update streaming example

This commit is contained in:
Maximilian Hils 2021-07-29 14:56:06 +02:00
parent b64005dfea
commit c7e8b82420
1 changed files with 7 additions and 4 deletions

View File

@ -7,14 +7,17 @@ Modifying streamed responses is tricky and brittle:
- If you want to replace all occurrences of "foobar", make sure to catch the cases - If you want to replace all occurrences of "foobar", make sure to catch the cases
where one chunk ends with [...]foo" and the next starts with "bar[...]. where one chunk ends with [...]foo" and the next starts with "bar[...].
""" """
from typing import Iterable, Union
def modify(chunks): def modify(data: bytes) -> Union[bytes, Iterable[bytes]]:
""" """
chunks is a generator that can be used to iterate over all chunks. This function will be called for each chunk of request/response body data that arrives at the proxy,
and once at the end of the message with an empty bytes argument (b"").
It may either return bytes or an iterable of bytes (which would result in multiple HTTP/2 data frames).
""" """
for chunk in chunks: return data.replace(b"foo", b"bar")
yield chunk.replace("foo", "bar")
def responseheaders(flow): def responseheaders(flow):