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

Ensure empty body response for 1xx/204/304 per RFC 9112 sec 6.3 (#7756) #7778

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Nov 2, 2023

Transfer-Encoding and Content-Length will be removed from the server response when sending a 1xx (Informational), 204 (No Content), or 304 (Not Modified) status since no body is expected for these status codes

We need to ensure Content-Length is kept for HEAD to ensure keep-alive works as expected and is allowed per
https://datatracker.ietf.org/doc/html/rfc2616#section-14.13

any recipient on the response chain (including the origin server) can
remove transfer codings when they are not needed.

aiohttp would incorrectly send an body of 0\r\n\r\n when chunked was set for Transfer-Encoding with the above status code.

This change ensures aiohttp does not send these invalid bodies.

The Transfer-Encoding and Content-Length headers will be removed when sending a 1xx (Informational), 204 (No Content), or 304 (Not Modified) except for HEAD responses since these status since no body is expected for these status codes.

An invalid body of 0\r\n\r\n will no longer be sent with these status codes.

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
  • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following: * .feature: Signifying a new feature. * .bugfix: Signifying a bug fix. * .doc: Signifying a documentation improvement. * .removal: Signifying a deprecation or removal of public API.
  • .misc: A ticket has been closed, but it is not of interest to users.
  • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

TODO:

  • figure out why client closes connection on HEAD requests with content length
  • figure out why client functional tests fail with AIOHTTP_NO_EXTENSIONS=1 test_keepalive_after_empty_body_status and test_keepalive_after_empty_body_status_stream_response -- They do not fail anymore after the fixed in
    Fix py http parser not treating 204/304/1xx as an empty body #7755
  • timeline
  • more functional testing

Co-authored-by: Sam Bull git@sambull.org
(cherry picked from commit f63cf18)

What do these changes do?

Are there changes in behavior for the user?

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

…libs#7756)

<!-- Thank you for your contribution! -->

`Transfer-Encoding` and `Content-Length` will be removed from the server
response when sending a 1xx (Informational), 204 (No Content), or 304
(Not Modified) status since no body is expected for these status codes

- `Content-Length` forbidden on 1xx/204 per
https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 and
discouraged on 304 per
https://datatracker.ietf.org/doc/html/rfc7232#section-4.1

We need to ensure `Content-Length` is kept for `HEAD` to ensure
keep-alive works as expected and is allowed per
https://datatracker.ietf.org/doc/html/rfc2616#section-14.13

- `Transfer-Encoding` removed per
https://datatracker.ietf.org/doc/html/rfc9112#section-6.1
> any recipient on the response chain (including the origin server) can
remove transfer codings when they are not needed.

`aiohttp` would incorrectly send an body of `0\r\n\r\n` when `chunked`
was set for `Transfer-Encoding` with the above status code.

This change ensures `aiohttp` does not send these invalid bodies.

The `Transfer-Encoding` and `Content-Length` headers will be removed
when sending a 1xx (Informational), 204 (No Content), or 304 (Not
Modified) except for `HEAD` responses since these status since no body
is expected for these status codes.

An invalid body of `0\r\n\r\n` will no longer be sent with these status
codes.

<!-- Are there any issues opened that will be resolved by merging this
change? -->

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [x] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."

TODO:

- [x] figure out why client closes connection on HEAD requests with
content length
- [x] figure out why client functional tests fail with
AIOHTTP_NO_EXTENSIONS=1 `test_keepalive_after_empty_body_status` and
`test_keepalive_after_empty_body_status_stream_response` -- They do not
fail anymore after the fixed in
aio-libs#7755
- [x] timeline
- [x] more functional testing

---------

Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit f63cf18)
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Nov 2, 2023
@bdraco
Copy link
Member Author

bdraco commented Nov 2, 2023

Pushed this PR to production and all is well

@bdraco bdraco marked this pull request as ready for review November 2, 2023 20:33
@bdraco bdraco requested a review from asvetlov as a code owner November 2, 2023 20:33
@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Nov 2, 2023

That's the second time pypy has segfaulted while building the dependencies today...
https://github.com/aio-libs/aiohttp/actions/runs/6737882623/job/18316282561?pr=7778

Copy link

codecov bot commented Nov 2, 2023

Codecov Report

Merging #7778 (5100372) into 3.9 (598334f) will increase coverage by 0.01%.
The diff coverage is 98.64%.

@@            Coverage Diff             @@
##              3.9    #7778      +/-   ##
==========================================
+ Coverage   97.31%   97.32%   +0.01%     
==========================================
  Files         107      107              
  Lines       32005    32203     +198     
  Branches     3735     3748      +13     
==========================================
+ Hits        31145    31343     +198     
  Misses        654      654              
  Partials      206      206              
Flag Coverage Δ
CI-GHA 97.24% <98.64%> (+0.01%) ⬆️
OS-Linux 96.93% <98.64%> (+0.01%) ⬆️
OS-Windows 94.46% <98.64%> (+0.03%) ⬆️
OS-macOS 96.58% <98.64%> (+0.01%) ⬆️
Py-3.10.11 94.38% <98.64%> (+0.03%) ⬆️
Py-3.10.13 96.76% <98.64%> (+0.01%) ⬆️
Py-3.11.6 96.46% <98.64%> (+0.02%) ⬆️
Py-3.8.10 94.35% <98.64%> (+0.03%) ⬆️
Py-3.8.18 96.70% <98.64%> (+0.02%) ⬆️
Py-3.9.13 94.36% <98.64%> (+0.03%) ⬆️
Py-3.9.18 96.74% <98.64%> (+0.01%) ⬆️
Py-pypy7.3.11 96.13% <98.64%> (+0.02%) ⬆️
VM-macos 96.58% <98.64%> (+0.01%) ⬆️
VM-ubuntu 96.93% <98.64%> (+0.01%) ⬆️
VM-windows 94.46% <98.64%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
aiohttp/helpers.py 95.20% <100.00%> (+0.03%) ⬆️
aiohttp/web_fileresponse.py 99.29% <100.00%> (ø)
tests/test_client_functional.py 98.48% <100.00%> (+0.06%) ⬆️
tests/test_helpers.py 98.92% <100.00%> (+0.04%) ⬆️
tests/test_web_functional.py 98.12% <100.00%> (+0.03%) ⬆️
tests/test_web_response.py 99.53% <100.00%> (+0.02%) ⬆️
aiohttp/web_response.py 97.92% <86.36%> (-0.18%) ⬇️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Dreamsorcerer Dreamsorcerer merged commit 9f43c38 into aio-libs:3.9 Nov 2, 2023
24 of 29 checks passed
xiangxli pushed a commit to xiangxli/aiohttp that referenced this pull request Dec 4, 2023
…libs#7756) (aio-libs#7778)

<!-- Thank you for your contribution! -->

`Transfer-Encoding` and `Content-Length` will be removed from the server
response when sending a 1xx (Informational), 204 (No Content), or 304
(Not Modified) status since no body is expected for these status codes

- `Content-Length` forbidden on 1xx/204 per
https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 and
discouraged on 304 per
https://datatracker.ietf.org/doc/html/rfc7232#section-4.1

We need to ensure `Content-Length` is kept for `HEAD` to ensure
keep-alive works as expected and is allowed per
https://datatracker.ietf.org/doc/html/rfc2616#section-14.13

- `Transfer-Encoding` removed per
https://datatracker.ietf.org/doc/html/rfc9112#section-6.1
> any recipient on the response chain (including the origin server) can
remove transfer codings when they are not needed.

`aiohttp` would incorrectly send an body of `0\r\n\r\n` when `chunked`
was set for `Transfer-Encoding` with the above status code.

This change ensures `aiohttp` does not send these invalid bodies.

The `Transfer-Encoding` and `Content-Length` headers will be removed
when sending a 1xx (Informational), 204 (No Content), or 304 (Not
Modified) except for `HEAD` responses since these status since no body
is expected for these status codes.

An invalid body of `0\r\n\r\n` will no longer be sent with these status
codes.

<!-- Are there any issues opened that will be resolved by merging this
change? -->

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [x] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
* ensure type is one of the following: * `.feature`: Signifying a new
feature. * `.bugfix`: Signifying a bug fix. * `.doc`: Signifying a
documentation improvement. * `.removal`: Signifying a deprecation or
removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."

TODO:

- [x] figure out why client closes connection on HEAD requests with
content length
- [x] figure out why client functional tests fail with
AIOHTTP_NO_EXTENSIONS=1 `test_keepalive_after_empty_body_status` and
`test_keepalive_after_empty_body_status_stream_response` -- They do not
fail anymore after the fixed in
aio-libs#7755
- [x] timeline
- [x] more functional testing

---------

Co-authored-by: Sam Bull <git@sambull.org>
(cherry picked from commit f63cf18)

<!-- Thank you for your contribution! -->

<!-- Please give a short brief about these changes. -->

<!-- Outline any notable behaviour for the end users. -->

<!-- Are there any issues opened that will be resolved by merging this
change? -->

- [ ] I think the code is well written
- [ ] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [ ] If you provide code modification, please add yourself to
`CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names.
- [ ] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
* if you don't have an `issue_id` change it to the pr id after creating
the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
* `.misc`: A ticket has been closed, but it is not of interest to users.
* Make sure to use full sentences with correct case and punctuation, for
example: "Fix issue with non-ascii contents in doctest text files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants