-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: Add models for websocket handlers for Tornado #20877
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Additional models for remote flow sources for `tornado.websocket.WebSocketHandler` have been added. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,8 @@ | |
| API::Node subclassRef() { | ||
| result = web().getMember("RequestHandler").getASubclass*() | ||
| or | ||
| result = WebSocket::WebSocketHandler::subclassRef() | ||
| or | ||
| result = ModelOutput::getATypeNode("tornado.web.RequestHandler~Subclass").getASubclass*() | ||
| } | ||
|
|
||
|
|
@@ -428,6 +430,42 @@ | |
| } | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // tornado.websocket | ||
| // --------------------------------------------------------------------------- | ||
| /** Gets a reference to the `tornado.websocket` module. */ | ||
| API::Node websocket() { result = Tornado::tornado().getMember("websocket") } | ||
|
|
||
| module WebSocket { | ||
| module WebSocketHandler { | ||
| /** Gets a reference to the `tornado.websocket.WebSocketHandler` class or any subclass. */ | ||
| API::Node subclassRef() { | ||
| result = websocket().getMember("WebSocketHandler").getASubclass*() | ||
| or | ||
| result = | ||
| ModelOutput::getATypeNode("tornado.websocket.WebSocketHandler~Subclass").getASubclass*() | ||
| } | ||
|
|
||
| class WebSocketHandlerClass extends Web::RequestHandler::RequestHandlerClass { | ||
| WebSocketHandlerClass() { this.getParent() = subclassRef().asSource().asExpr() } | ||
|
|
||
| override Function getARequestHandler() { | ||
| result = super.getARequestHandler() | ||
| or | ||
| result = this.getAMethod() and | ||
| result.getName() = "open" | ||
| } | ||
|
|
||
| /** Gets a function that could handle incoming websocket events, if any. */ | ||
| Function getAWebSocketEventHandler() { | ||
| result = this.getAMethod() and | ||
| result.getName() = | ||
| ["on_message", "on_close", "on_ping", "on_pong", "select_subprotocol", "check_origin"] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -542,6 +580,27 @@ | |
| override string getFramework() { result = "Tornado" } | ||
| } | ||
|
|
||
| /** A request handler for WebSocket events */ | ||
|
||
| private class TornadoWebSocketEventHandler extends Http::Server::RequestHandler::Range { | ||
| TornadoWebSocketEventHandler() { | ||
| exists(TornadoModule::WebSocket::WebSocketHandler::WebSocketHandlerClass cls | | ||
| cls.getAWebSocketEventHandler() = this | ||
| ) | ||
| } | ||
|
|
||
| override Parameter getARoutedParameter() { | ||
| // The `open` method is handled as a normal request handler in `TornadoRouteSetup` or `TornadoRequestHandlerWithoutKnownRoute`. | ||
| // For other event handlers (such as `on_message`), all parameters should be remote flow sources, as they are not affected by routing. | ||
| result in [ | ||
| this.getArg(_), this.getArgByName(_), this.getVararg().(Parameter), | ||
| this.getKwarg().(Parameter) | ||
| ] and | ||
| not result = this.getArg(0) | ||
| } | ||
|
|
||
| override string getFramework() { result = "Tornado" } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Response modeling | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,27 @@ class PossiblyNotRouted(tornado.web.RequestHandler): | |||||
| def get(self): # $ requestHandler | ||||||
| self.write("NotRouted") # $ HttpResponse | ||||||
|
|
||||||
| class WebSocket(tornado.websocket.WebSocketHandler): | ||||||
|
||||||
| def open(self, x): # $ requestHandler routedParameter=x | ||||||
| self.write_message("WebSocket open {}".format(x)) | ||||||
|
||||||
| self.write_message("WebSocket open {}".format(x)) | |
| self.write_message("WebSocket open {}".format(x)) |
Copilot
AI
Nov 20, 2025
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.
Trailing whitespace should be removed from this blank line.
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 term "websocket" should be capitalized as "WebSocket" to maintain consistency with line 583 and adhere to the standard capitalization of the WebSocket protocol name.