Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: do not send content on HTTPError(204) #3361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pawciobiel
Copy link

@pawciobiel pawciobiel commented Feb 16, 2024

Change write_error() to not send body if
status code in (204, 304) or (100 <= status code < 200),
refactored into RequestHandler._should_not_send_content(status_code)
Change get_arguments() to raise ValueError if strip is not boolean, as opposed to AssertionError.

Fixes #3360

Change `write_error()` to not send body if
 status code in (204, 304) or (100 <= status code < 200)
 - refactored into `RequestHandler._should_not_send_content(status_code)`
Change `get_arguments()` to raise `ValueError` if `strip` is not boolean
 - as opposed to `AssertionError`.

Fixes tornadoweb#3360
Copy link
Member

@bdarnell bdarnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One other thing to consider is that raise HTTPError(204) will be logged as a warning in log_exception. HTTPError was meant to be used for errors, not just a shorthand for "set status and return". We have another way to do that: self.set_status(204); raise tornado.web.Finish().

@@ -1319,6 +1323,8 @@ def write_error(self, status_code: int, **kwargs: Any) -> None:
for line in traceback.format_exception(*kwargs["exc_info"]):
self.write(line)
self.finish()
elif self._should_not_send_content(status_code):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the first branch in the if: even in serve_traceback mode, it's inappropriate to send it in a response to 204/304.

@@ -466,7 +466,8 @@ def get_arguments(self, name: str, strip: bool = True) -> List[str]:
# Make sure `get_arguments` isn't accidentally being called with a
# positional argument that's assumed to be a default (like in
# `get_argument`.)
assert isinstance(strip, bool)
if not isinstance(strip, bool):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly agree with getting rid of the assert statement, but this seems like a case where it's warranted - if it's relevant it'll fire all the time so you'll see it in development (or even better, a type checker can catch it statically), and then you can disable the assertion in production with -O

@bdarnell bdarnell added the web label Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Send 204 as HTTPError
2 participants