gh-66077: Fix blocking reads from wsgi.input in wsgiref.simple_server#148803
Open
bangseongbeom wants to merge 1 commit intopython:mainfrom
Open
gh-66077: Fix blocking reads from wsgi.input in wsgiref.simple_server#148803bangseongbeom wants to merge 1 commit intopython:mainfrom
wsgi.input in wsgiref.simple_server#148803bangseongbeom wants to merge 1 commit intopython:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates wsgiref.simple_server to comply with PEP 3333’s wsgi.input semantics by preventing blocking reads past the request body, and aligns wsgiref.validate with the spec allowing read() to be called with no arguments.
Changes:
- Add an input-stream wrapper in
wsgiref.simple_serverthat enforcesContent-Lengthas an EOF boundary forwsgi.input. - Update
wsgiref.validateto allowwsgi.input.read()with zero or one argument, and adjust tests accordingly. - Document the new
WSGIRequestHandler.get_stdin()hook and add integration tests coveringread,readline,readlines, and iteration behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Misc/NEWS.d/next/Library/2026-04-21-01-58-47.gh-issue-66077.dvcX2i.rst | Adds a NEWS entry describing the wsgi.input blocking fix. |
| Lib/wsgiref/validate.py | Updates validator to permit wsgi.input.read() without arguments. |
| Lib/wsgiref/simple_server.py | Introduces InputWrapper and routes wsgi.input through get_stdin() when Content-Length is present. |
| Lib/test/test_wsgiref.py | Adds/updates integration tests to cover the new wsgi.input behaviors. |
| Doc/library/wsgiref.rst | Documents WSGIRequestHandler.get_stdin() behavior and override point. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+143
to
+148
| def get_stdin(self): | ||
| length = self.headers.get('content-length') | ||
| if length: | ||
| return InputWrapper(self.rfile, int(length)) | ||
| else: | ||
| return self.rfile |
Contributor
Author
There was a problem hiding this comment.
According to RFC 9110, Content-Length must be an integer number. Do we need to handle cases where it is not an integer as an exception?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The existing implementation of
wsgiref.simple_serverdoes not properly comply with the requirements in PEP 3333:To satisfy these requirements, this PR introduces
InputWrapper. This class is instantiated when the request includes aContent-Length, and is responsible for treatingContent-Lengthas the end of the input stream.In addition, since PEP 3333 allows
read()to be called without an argument, I updatedwsgiref.validateand the related tests accordingly.I also added a few tests, though I'm not completely sure they were added correctly. I'd appreciate it if you could take a look.
📚 Documentation preview 📚: https://cpython-previews--148803.org.readthedocs.build/