-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow case-insensitive HTTP Upgrade header #2097
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2097 +/- ##
=========================================
Coverage 92.253% 92.253%
=========================================
Files 38 38
Lines 3485 3485
Branches 583 583
=========================================
Hits 3215 3215
Misses 183 183
Partials 87 87
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scheme check: headers
is not defined, you should use self.headers
.
Looks great. I think it might've been done this way (with just lowercase "websockets") because the Thanks for this fix. I'm surprised we havent had reports of broken websockets in Sanic v21.3 due to this. |
Ah, type checking has picked up a valid error case.
I know it's a pain and an extra step, but you probably need to put a |
I agree. I am surprised as well since this has certainly been tested and out in usage already. Which kind of makes me curious as to why we haven't seen this already. Ideally I'd also like to have a test coverage for it, but that might be hard to do with our current test client. |
Thank you for the fast response and quick review. |
Interesting. I did a bit of digging. In the GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13 So does the documentation of the Mozilla Dev page. Running a quick inspect on Websocket using Firefox indicates the same. This is the same behavior on chrome as well. |
sanic/http.py
Outdated
@@ -218,7 +218,7 @@ async def http1_request_header(self): | |||
raise InvalidUsage("Bad Request") | |||
|
|||
headers_instance = Header(headers) | |||
self.upgrade_websocket = headers_instance.get("upgrade") == "websocket" | |||
self.upgrade_websocket = "upgrade" in headers_instance and headers_instance.get("upgrade", None) is not None and headers_instance.get("upgrade").lower() == "websocket" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we really want to do this check the easier option might be
self.upgrade_websocket = "upgrade" in headers_instance and headers_instance.get("upgrade", None) is not None and headers_instance.get("upgrade").lower() == "websocket" | |
self.upgrade_websocket = headers_instance.get("upgrade", "").lower() == "websocket" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this better
I don't know whats going on with those travis tests. It should be passing now. I'll try to restart them. |
Nope, it won't let me restart it. (Error: "Oops, this Job cannot be Restarted.") I think we can just merge this regardless. On a different note, for @sanic-org/core-devs |
Those tests I've been really flaky lately. Especially the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving this change. But I do like the idea of using getone
as suggested by @ashleysommer.. May be it can be a total cleanup work across the board using a different issue item?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there is no new line at the end of the files. It should be failing linting. Can you run make pretty
to make sure that it complies with black
. You can also run tox -e lint
to make sure that everything is formatted properly.
+1 the explicit usage of |
Are you sure that this is the actual cause of the failing tests? As far as I see, there is generally no new line at the end of files in this project. I feel like this is more related to #2092 because it's only |
Looks like Travis has no complaints this time 🎉 |
Added a tracker item for this |
|
As far as I understand Section 11.2 of RFC 6455 ("The WebSocket Protocol"), the HTTP Upgrade header should actually be
WebSocket
. At least Firefox uses the header like that, which leads to a wrong detection of the underlying protocol in Sanic (e.g. timeouts after a minute because Sanic thinks the request is a normal HTTP request). This is why I propose to make the check for the Upgrade header case-insensitive in order to correctly handle the incoming request.