Skip to content

modify headers and body in place - #3335

Merged
davidism merged 2 commits into
mainfrom
apply-status
Oct 22, 2026
Merged

davidism merged 2 commits into
mainfrom
apply-status

Conversation

@davidism

@davidism davidism commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

closes #3320, related to #3281

Added apply_status (mirroring apply_conditions and apply_range) that modifies the headers and body based on the status. apply_conditions and get_wsgi_response call it automatically. get_wsgi_headers and get_app_iter are deprecated.

Despite #3281 wanting to pass request instead of environ where possible, I decided to keep environ for get_wsgi_response. It's almost always from __call__(environ, start_response), where we can't change that signature because its part of WSGI. Constructing a request there just for this would be sort of pointless, although it's not expensive. We can do it later if we want.

Also decided not to pass request (or environ) to apply_status. First, it would require constructing a request from get_wsgi_response, just discussed why not that. Second, no adjustments currently depend on the request, only "don't send a body on HEAD".

Decided "don't send a body on HEAD" shouldn't modify the body in place, because then it would truly make caching responses useless, as it would always break a simple reused GET response automatically.

The 205 status clears content-length and body, like 1XX, 204, and 304. This seems to have been overlooked and never reported, but might as well be consistent.

set_data calls the existing iterable's close method. This was missed in #3325

Inspired by Quart, make_sequence will join all items into one bytes value and call set_data, which also sets content_length. PEP 3333 says this is the preferred way to return simple responses, len > 1 iterators are meant for streaming. Unlike Quart, len > 1 sequences are left as-is, only non-sequences are converted, which matches existing behavior.

freeze is deprecated. Added a new docs section explaining why responses can't be reused in general.

_ensure_sequence mutable parameter is deprecated. This was missed in #3325

Updated docstrings. Clarified that close and call_on_close are not called if direct_passthrough is enabled.

@davidism davidism added this to the 3.2.0 milestone Oct 22, 2026
@davidism
davidism merged commit ef9c58f into main Oct 22, 2026
12 checks passed
@davidism
davidism deleted the apply-status branch October 22, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move header and body checks to sans-IO Response

1 participant