-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for continuation frame in WebSocketMessageParser #2320
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
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||||||||||||
| #include <json/value.h> | ||||||||||||||||
| #include <json/writer.h> | ||||||||||||||||
| #include <thread> | ||||||||||||||||
| #include <limits> | ||||||||||||||||
|
|
||||||||||||||||
| using namespace drogon; | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -268,14 +269,14 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) | |||||||||||||||
| { | ||||||||||||||||
| // According to the rfc6455 | ||||||||||||||||
| gotAll_ = false; | ||||||||||||||||
| if (buffer->readableBytes() >= 2) | ||||||||||||||||
| while (buffer->readableBytes() >= 2) | ||||||||||||||||
| { | ||||||||||||||||
| unsigned char opcode = (*buffer)[0] & 0x0f; | ||||||||||||||||
| bool isControlFrame = false; | ||||||||||||||||
| switch (opcode) | ||||||||||||||||
| { | ||||||||||||||||
| case 0: | ||||||||||||||||
| // continuation frame | ||||||||||||||||
| LOG_TRACE << "continuation frame"; | ||||||||||||||||
| break; | ||||||||||||||||
| case 1: | ||||||||||||||||
| type_ = WebSocketMessageType::Text; | ||||||||||||||||
|
|
@@ -327,8 +328,13 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) | |||||||||||||||
| { | ||||||||||||||||
| indexFirstMask = 10; | ||||||||||||||||
| } | ||||||||||||||||
| if (indexFirstMask > 2 && buffer->readableBytes() >= indexFirstMask) | ||||||||||||||||
| if (indexFirstMask > 2) | ||||||||||||||||
| { | ||||||||||||||||
| if (buffer->readableBytes() < indexFirstMask) | ||||||||||||||||
| { | ||||||||||||||||
| // Not enough data yet, wait for more. | ||||||||||||||||
| return true; | ||||||||||||||||
| } | ||||||||||||||||
| if (isControlFrame) | ||||||||||||||||
| { | ||||||||||||||||
| // rfc6455-5.5 | ||||||||||||||||
|
|
@@ -344,14 +350,17 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) | |||||||||||||||
| } | ||||||||||||||||
| else if (indexFirstMask == 10) | ||||||||||||||||
| { | ||||||||||||||||
| length = (unsigned char)(*buffer)[2]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[3]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[4]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[5]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[6]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[7]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[8]; | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[9]; | ||||||||||||||||
| length = 0; | ||||||||||||||||
| for (int i = 2; i <= 9; ++i) | ||||||||||||||||
| { | ||||||||||||||||
| if (length > ((std::numeric_limits<size_t>::max)() >> 8)) | ||||||||||||||||
|
||||||||||||||||
| { | ||||||||||||||||
| LOG_ERROR | ||||||||||||||||
| << "Payload length too large to handle safely"; | ||||||||||||||||
| return false; | ||||||||||||||||
| } | ||||||||||||||||
| length = (length << 8) + (unsigned char)(*buffer)[i]; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| else | ||||||||||||||||
| { | ||||||||||||||||
|
|
@@ -380,9 +389,16 @@ bool WebSocketMessageParser::parse(trantor::MsgBuffer *buffer) | |||||||||||||||
| { | ||||||||||||||||
| message_[oldLen + i] = (rawData[i] ^ masks[i % 4]); | ||||||||||||||||
| } | ||||||||||||||||
|
||||||||||||||||
| } | |
| } | |
| // Consume the entire frame from the buffer, regardless of the FIN flag. | |
| // This is necessary to process subsequent frames correctly, as the WebSocket | |
| // protocol allows fragmented messages. The `message_` buffer accumulates | |
| // data from all frames, and the `isFin` flag determines when the message | |
| // is complete. |
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.
Consider adding a comment to clarify that the while loop is intentionally used to process multiple frames in a single parse call, ensuring that continuation frames are handled correctly.