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

Add EmptyResponse class #1178

Closed
declaresub opened this issue May 5, 2021 · 3 comments · Fixed by #1397
Closed

Add EmptyResponse class #1178

declaresub opened this issue May 5, 2021 · 3 comments · Fixed by #1397
Labels
clean up Refinement to existing functionality

Comments

@declaresub
Copy link

HTTP responses with status codes 1xx, 204, 205, and 304 are special cases. 1xx, 204, 304 responses must not include a message body. The empty message body for 205 responses can be handled in a few ways, but the only one that would make sense for Starlette would be to add a Content-Length header with value 0.

Currently it is not at all obvious to users how to implement these responses. Starlette can make it explicit with the addition of an EmptyResponse class that does the right thing for each of the status codes cited above.

The "right thing" differs by status code. The key difference is

  • 1xx, 204: must not have a Content-Length header
  • 205: should have Content-Length: 0
  • 304: may have a Content-Length header if the value equals the length of the corresponding 200 response that could have been sent.

Thus, an EmptyResponse object should ensure no Content-Length header in the first case, ensure Content-Length: 0 in the second case, and pass along a Content-Length header if supplied in the third case; here it is the caller's responsibility to get the value right.

In all cases, an EmptyResponse object would send an 'http.response.body' event with body: b'', more_body: False so that the ASGI server knows to begin sending the response.

The addition of an EmptyResponse class should resolve at least one open issue (#1099).

@JayH5
Copy link
Member

JayH5 commented Aug 16, 2021

Initially I wasn't sure about this, but I'm warming up to the idea of an explicit EmptyResponse that handles these common cases correctly. So:

  1. "The "right thing" differs by status code.": do we have some RFCs we can point to as proof?
  2. I'd be up for reviewing a PR for this, or may give it a try myself if/when I find time.

@declaresub
Copy link
Author

@tomchristie
Copy link
Member

Closing in favour of #1397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean up Refinement to existing functionality
Projects
None yet
3 participants