Skip to content

Commit 3a9d54f

Browse files
committed
Merge branch 'stable'
2 parents c34d6e8 + a29f88c commit 3a9d54f

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

docs/patterns/streaming.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ roundtrip to the filesystem?
88

99
The answer is by using generators and direct responses.
1010

11+
HTTP Response Behavior
12+
----------------------
13+
14+
**Headers cannot be changed after the streaming response starts.**
15+
16+
When using streaming, it's important to be aware of the order than an HTTP
17+
response is sent. All headers must be sent first, then the body. More headers
18+
cannot be sent after the body has begun. Therefore, you must make sure all
19+
headers are set before starting the response, outside the generator.
20+
21+
In particular, if the generator will access ``session``, be sure to do so in the
22+
view as well so that the ``Vary: cookie`` header will be set. Do not modify the
23+
session in the generator, as the ``Set-Cookie`` header will already be sent.
24+
25+
1126
Basic Usage
1227
-----------
1328

docs/templating.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,11 @@ functions to make this easier to use.
251251
return stream_template("timeline.html")
252252
253253
These functions automatically apply the
254-
:func:`~flask.stream_with_context` wrapper if a request is active, so
255-
that it remains available in the template.
254+
:func:`~flask.stream_with_context` wrapper if a request is active, so that
255+
:data:`.request`, :data:`.session`, and :data:`.g` remain available in the
256+
template.
257+
258+
More headers cannot be sent after the body has begun. Therefore, you must
259+
make sure all headers are set before starting the response. In particular,
260+
if the template will access ``session``, be sure to do so in the view as
261+
well so that the ``Vary: cookie`` header will be set.

src/flask/helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def stream_with_context(
6868
available, even though at the point the generator runs the request context
6969
will typically have ended.
7070
71+
.. warning::
72+
73+
Due to the following caveat, it is often safer to pass the data you
74+
need as arguments to the generator, rather than relying on the context
75+
objects.
76+
77+
More headers cannot be sent after the body has begun. Therefore, you must
78+
make sure all headers are set before starting the response. In particular,
79+
if the generator will access ``session``, be sure to do so in the view as
80+
well so that the ``Vary: cookie`` header will be set. Do not modify the
81+
session in the generator, as the ``Set-Cookie`` header will already be sent.
82+
7183
Use it as a decorator on a generator function:
7284
7385
.. code-block:: python

0 commit comments

Comments
 (0)