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

add regex support for websocket controller #1777 #1778 #1779

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

TYUTthelastone
Copy link
Contributor

#1777

刚用github,不太会提代码,点半天不知道在点啥,最后只能删了重来...💀

WS_PATH_LIST_BEGIN
    WS_PATH_ADD("/chat", Get);
    WS_PATH_ADD_REGEX("/[^/]*", Get, Post, Options);
WS_PATH_LIST_END

add additional regular regex strings support for the WebSocket controller.

@TYUTthelastone TYUTthelastone changed the title add regex support for websocket controller #1777 add regex support for websocket controller #1777 #1778 Sep 15, 2023
@an-tao
Copy link
Member

an-tao commented Sep 15, 2023

WS_PATH_ADD("/chat", "drogon::LocalHostFilter", Get);

请在服务端和客户端添加适当的测试代码,谢谢~

@ken-matsui ken-matsui removed their request for review September 15, 2023 22:02
@ken-matsui
Copy link
Member

@an-tao Sorry. I have been so busy that I cannot review this PR. Can you assign another reviewer?

@an-tao
Copy link
Member

an-tao commented Sep 16, 2023

@an-tao Sorry. I have been so busy that I cannot review this PR. Can you assign another reviewer?

Okey, thanks.

@Mis1eader-dev
Copy link
Member

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

@Mis1eader-dev
Copy link
Member

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 WS_PATH_ADD and WS_PATH_ADD_REGEX, because in one of the previous commits any websocket request that is not a GET request has been disallowed.

@@ -184,16 +244,35 @@ void WebsocketControllersRouter::route(
std::string wsKey = req->getHeaderBy("sec-websocket-key");
if (!wsKey.empty())
{
std::smatch result;
Copy link
Member

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

Comment on lines 83 to 93
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";
Copy link
Member

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.

@Mis1eader-dev
Copy link
Member

Seems we have diverged in files, resolving this merge conflict requires moving functions around

@Mis1eader-dev
Copy link
Member

I have now come to a point where I also need this feature, any chance for us to revive this PR?

@TYUTthelastone
Copy link
Contributor Author

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.

@an-tao
Copy link
Member

an-tao commented May 31, 2024

@TYUTthelastone Thanks so much. please pay attention to the questions I reminded in this PR.
image
image

@Mis1eader-dev
Copy link
Member

This needs a rebase, it has pulled many other commits with itself.

@TYUTthelastone TYUTthelastone changed the base branch from cpp14 to master June 1, 2024 07:31
@Mis1eader-dev
Copy link
Member

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

Copy link
Member

@Mis1eader-dev Mis1eader-dev left a 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.

@an-tao an-tao merged commit 9a96a20 into drogonframework:master Jun 4, 2024
34 checks passed
@@ -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);
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use the same format as the http using
{6E310ABF-9D6D-4890-8237-33A8616C4234}

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants