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

Fix support for connection Upgrade and CONNECT when some data in the stream has been read. #882

Merged
merged 10 commits into from
Feb 20, 2024

Conversation

MtkN1
Copy link
Contributor

@MtkN1 MtkN1 commented Feb 11, 2024

Summary

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@MtkN1
Copy link
Contributor Author

MtkN1 commented Feb 11, 2024

I have created a draft pull request to begin the work!
First, Upgrade/CONNECT (incorrectly) raises an exception.

My understanding is that the following work needs to be done...

  1. Implement a network stream that can handle trailing data like this advice (Trailing Data in WebSocket Upgrade Requests #871 (comment))
  2. Generates its network stream at the step that raised the exception
  3. Switch variables to be stored in network_stream.
    "network_stream": self._network_stream,

@tomchristie I would appreciate any advice you can give me!

@tomchristie
Copy link
Member

tomchristie commented Feb 11, 2024

Ah fantastic thanks, shall we start with a test?...

  1. There's an existing Upgrade test here, that could be replicated into a new test_upgrade_with_trailing_data test case.

async def test_http11_upgrade_connection():

I think the next step after that will be...

  1. We probably want to change receive_response_headers so that it also returns the trailing_data as part of the response info, include the trailing data in the raised exception.

And then...

  1. We'll want an HTTPUpgradeStream(SyncNetworkStream) class. This have an __init__ method taking a network stream and trailing data. Most of the methods should delegate to the underlying network stream, except read which should initially return the trailing data. We'll return that in the extensions for the Upgrade/CONNECT cases.

@MtkN1
Copy link
Contributor Author

MtkN1 commented Feb 12, 2024

Thank you for your guidance 🙏 I'll give it a try.

@MtkN1 MtkN1 marked this pull request as ready for review February 14, 2024 08:15
@MtkN1
Copy link
Contributor Author

MtkN1 commented Feb 14, 2024

Work completed, I marked PR as ready. Could you please review this?

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

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

This one is a fantastic fix, thanks. ✨
Minor suggestion wrt. the tests.


Later follow-ups we can consider...

  • Should all HTTP requests include this behaviour or only Upgrade/CONNECT?
  • Should all HTTP requests return network_stream or only Upgrade/CONNECT?
  • What might a nice __repr__ be here?

(I'm just jotting these down here for reference)

httpcore/_async/http11.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@tomchristie tomchristie changed the title Support connection Upgrade and CONNECT. Fix support for connection Upgrade and CONNECT when some data in the stream has been read. Feb 14, 2024
@tomchristie tomchristie added the bug Something isn't working label Feb 14, 2024
Co-authored-by: Tom Christie <tom@tomchristie.com>
Copy link
Contributor

@T-256 T-256 left a comment

Choose a reason for hiding this comment

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

LGTM, Thank You!

tests/_async/test_http11.py Outdated Show resolved Hide resolved
Co-authored-by: T-256 <132141463+T-256@users.noreply.github.com>
@tomchristie
Copy link
Member

Nice work.

@MtkN1
Copy link
Contributor Author

MtkN1 commented Feb 20, 2024

Thank you for your approval 🚀 It is a pleasure to contribute to this project.

@tomchristie tomchristie merged commit accae7b into encode:master Feb 20, 2024
5 checks passed
@tomchristie tomchristie mentioned this pull request Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support connection Upgrade and CONNECT.
3 participants