This commit is contained in:
Youhei Sakurai 2015-02-27 02:44:47 +09:00
parent bd6c3f64c1
commit 77bb0b74ed
3 changed files with 20 additions and 1 deletions

View File

@ -40,6 +40,9 @@ Responses that should be tagged for streaming by setting their respective .strea
$!example("examples/stream.py")!$
In addition, if the .stream attribute is set to callable(), .stream will work as a hook in chunk data processing.
$!example("examples/stream_modify.py")!$
<h2>Implementation Details</h2>

11
examples/stream_modify.py Normal file
View File

@ -0,0 +1,11 @@
def modify(chunks):
"""
chunks is a generator that can be used to iterate over all chunks.
Each chunk is a (prefix, content, suffix) tuple.
For example, in the case of chunked transfer encoding: ("3\r\n","foo","\r\n")
"""
for prefix, content, suffix in chunks:
yield prefix, content.replace("foo","bar"), suffix
def responseheaders(ctx, flow):
flow.response.stream = modify

View File

@ -1332,7 +1332,12 @@ class HTTPHandler(ProtocolHandler):
# incrementally:
h = flow.response._assemble_head(preserve_transfer_encoding=True)
self.c.client_conn.send(h)
for chunk in http.read_http_body_chunked(self.c.server_conn.rfile,
for chunk in hasattr(flow.response.stream, "__call__") and \
flow.response.stream(http.read_http_body_chunked(self.c.server_conn.rfile,
flow.response.headers,
self.c.config.body_size_limit, flow.request.method,
flow.response.code, False, 4096)) or \
http.read_http_body_chunked(self.c.server_conn.rfile,
flow.response.headers,
self.c.config.body_size_limit, flow.request.method,
flow.response.code, False, 4096):