-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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 regex support for websocket controller #1777 #1778 #1779
Conversation
请在服务端和客户端添加适当的测试代码,谢谢~ |
@an-tao Sorry. I have been so busy that I cannot review this PR. Can you assign another reviewer? |
Okey, thanks. |
Causes a segmentation fault when matches the regex: let ws = new WebSocket("ws://127.0.0.1:8848/chat"); // fine
let ws = new WebSocket("ws://127.0.0.1:8848/chatting"); // segfault
let ws = new WebSocket("ws://127.0.0.1:8848"); // segfault |
One thing to note, according to the WebSocket specification websockets should accept websocket requests using GET method only, I suggest we remove support for allowing methods to be passed in to |
@@ -184,16 +244,35 @@ void WebsocketControllersRouter::route( | |||
std::string wsKey = req->getHeaderBy("sec-websocket-key"); | |||
if (!wsKey.empty()) | |||
{ | |||
std::smatch result; |
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.
Prefer putting this in its respective scope, within if (iter == wsCtrlMap_.end())
block just before the for loop
std::string messageType = "Unknown"; | ||
if (type == WebSocketMessageType::Text) | ||
messageType = "text"; | ||
else if (type == WebSocketMessageType::Pong) | ||
messageType = "pong"; | ||
else if (type == WebSocketMessageType::Ping) | ||
messageType = "ping"; | ||
else if (type == WebSocketMessageType::Binary) | ||
messageType = "binary"; | ||
else if (type == WebSocketMessageType::Close) | ||
messageType = "Close"; |
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 think we can use std::string_view instead to avoid heap allocation.
Seems we have diverged in files, resolving this merge conflict requires moving functions around |
I have now come to a point where I also need this feature, any chance for us to revive this PR? |
I notice that this code has been removed in the last version, I'll find time to push my code again later. |
@TYUTthelastone Thanks so much. please pay attention to the questions I reminded in this PR. |
This needs a rebase, it has pulled many other commits with itself. |
I tested it out, throws a segfault @TYUTthelastone, can you please check if it works properly? 20240601 17:36:41.431205 UTC 8794 TRACE [run] Start to run... - HttpAppFrameworkImpl.cc:521
20240601 17:36:41.433687 UTC 8794 TRACE [createListeners] thread num=1 - ListenerManager.cc:84
20240601 17:36:41.433923 UTC 8794 TRACE [createNonblockingSocketOrDie] sock=11 - Socket.h:46
20240601 17:36:41.433984 UTC 8794 TRACE [IgnoreSigPipe] Ignore SIGPIPE - TcpServer.h:298
20240601 17:36:41.433997 UTC 8794 TRACE [~TcpServer] TcpServer::~TcpServer [drogonPortTest] destructing - TcpServer.cc:47
20240601 17:36:41.434042 UTC 8794 TRACE [~Socket] Socket deconstructed:11 - Socket.cc:245
20240601 17:36:41.434093 UTC 8794 TRACE [createNonblockingSocketOrDie] sock=10 - Socket.h:46
20240601 17:36:41.434130 UTC 8794 TRACE [IgnoreSigPipe] Ignore SIGPIPE - TcpServer.h:298
20240601 17:36:41.434647 UTC 8794 TRACE [start] HttpServer[drogon] starts listening on 0.0.0.0:8080 - HttpServer.cc:88
20240601 17:36:41.434869 UTC 8796 TRACE [operator()] map size=1 - TcpServer.cc:131
20240601 17:36:44.046592 UTC 8796 TRACE [newConnection] new connection:fd=11 address=127.0.0.1:45580 - TcpServer.cc:62
20240601 17:36:44.046656 UTC 8796 TRACE [TcpConnectionImpl] new connection:127.0.0.1:45580->127.0.0.1:8080 - TcpConnectionImpl.cc:78
20240601 17:36:44.046702 UTC 8796 TRACE [operator()] connectEstablished - TcpConnectionImpl.cc:262
20240601 17:36:44.046776 UTC 8796 TRACE [addHeader] cookies!!!:null; - HttpRequestImpl.cc:424
20240601 17:36:44.046822 UTC 8796 TRACE [isWebSocket] new websocket request - HttpServer.cc:1045 |
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.
@an-tao looks good to me and works properly.
@@ -14,6 +14,7 @@ class WebSocketChat : public drogon::WebSocketController<WebSocketChat> | |||
const WebSocketConnectionPtr &) override; | |||
WS_PATH_LIST_BEGIN | |||
WS_PATH_ADD("/chat", Get); | |||
WS_ADD_PATH_VIA_REGEX("/[^/]*", Get); |
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.
@an-tao the naming feels strange on the macro, I say we make it have the same prefix as the original and add "_VIA_REGEX"
to it:
WS_PATH_ADD_VIA_REGEX
What do you say?
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.
What about WS_PATH_REGEX_ADD
? This corresponds to the style of WS_PATH_LIST_BEGIN
as well, i.e., WS_
+ Noun + Verb.
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.
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.
Damn, and making naming changes to macros will break compatibility.
We can stick to one standard naming convention, and keep the old ones for compatibility purposes.
My suggestion is to use scoped naming convention:
PATH_ADD
PATH_ADD_VIA_REGEX
METHOD_ADD
METHOD_ADD_TO
METHOD_ADD_VIA_REGEX
WS_ADD
WS_ADD_VIA_REGEX
#1777
add additional regular regex strings support for the WebSocket controller.