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

Fix continuation frame interrupting subsequent reads #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/oatpp-websocket/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ void AsyncWebSocket::setConfig(const Config& config) {
}

bool AsyncWebSocket::checkForContinuation(const Frame::Header& frameHeader) {
if(m_lastOpcode == Frame::OPCODE_TEXT || m_lastOpcode == Frame::OPCODE_BINARY) {
return false;
}
bool flag = m_lastOpcode == -1;
if(frameHeader.fin) {
m_lastOpcode = -1;
} else {
} else if(frameHeader.opcode != Frame::OPCODE_CONTINUATION && flag) {
m_lastOpcode = frameHeader.opcode;
}
return true;
return flag;
}

oatpp::async::CoroutineStarter AsyncWebSocket::readFrameHeaderAsync(const std::shared_ptr<Frame::Header>& frameHeader) {
Expand Down Expand Up @@ -353,24 +351,28 @@ oatpp::async::CoroutineStarter AsyncWebSocket::handleFrameAsync(const std::share

switch (m_frameHeader->opcode) {
case Frame::OPCODE_CONTINUATION:
if(m_socket->m_lastOpcode < 0) {
throw std::runtime_error("[oatpp::web::protocol::websocket::AsyncWebSocket::handleFrameAsync(){HandleFrameCoroutine}]: Invalid communication state.");
if(m_socket->checkForContinuation(*m_frameHeader)) {
throw std::runtime_error("[oatpp::web::protocol::websocket::AsyncWebSocket::handleFrameAsync(){HandleFrameCoroutine}]: Invalid communication state. OPCODE_CONTINUATION unexpected");
} else {
return m_socket->readPayloadAsync(m_frameHeader, nullptr).next(finish());
}
return m_socket->readPayloadAsync(m_frameHeader, nullptr).next(finish());
break;

case Frame::OPCODE_TEXT:
if(m_socket->checkForContinuation(*m_frameHeader)) {
return m_socket->readPayloadAsync(m_frameHeader, nullptr).next(finish());
} else {
throw std::runtime_error("[oatpp::web::protocol::websocket::AsyncWebSocket::handleFrameAsync(){HandleFrameCoroutine}]: Invalid communication state. OPCODE_CONTINUATION expected");
}
break;

case Frame::OPCODE_BINARY:
if(m_socket->checkForContinuation(*m_frameHeader)) {
return m_socket->readPayloadAsync(m_frameHeader, nullptr).next(finish());
} else {
throw std::runtime_error("[oatpp::web::protocol::websocket::AsyncWebSocket::handleFrameAsync(){HandleFrameCoroutine}]: Invalid communication state. OPCODE_CONTINUATION expected");
}
break;

case Frame::OPCODE_CLOSE:
m_shortMessageStream = std::make_shared<data::stream::BufferOutputStream>();
Expand Down
15 changes: 7 additions & 8 deletions src/oatpp-websocket/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ void WebSocket::setConfig(const Config& config) {
}

bool WebSocket::checkForContinuation(const Frame::Header& frameHeader) {
if(m_lastOpcode == Frame::OPCODE_TEXT || m_lastOpcode == Frame::OPCODE_BINARY) {
return false;
}
bool flag = m_lastOpcode == -1;
if(frameHeader.fin) {
m_lastOpcode = -1;
} else {
} else if(flag && frameHeader.opcode != Frame::OPCODE_CONTINUATION) {
m_lastOpcode = frameHeader.opcode;
}
return true;
return flag;
}

void WebSocket::readFrameHeader(Frame::Header& frameHeader) const {
Expand Down Expand Up @@ -203,10 +201,11 @@ void WebSocket::handleFrame(const Frame::Header& frameHeader) {

switch (frameHeader.opcode) {
case Frame::OPCODE_CONTINUATION:
if(m_lastOpcode < 0) {
throw std::runtime_error("[oatpp::web::protocol::websocket::WebSocket::handleFrame()]: Invalid communication state.");
if(checkForContinuation(frameHeader)) {
throw std::runtime_error("[oatpp::web::protocol::websocket::WebSocket::handleFrame()]: Invalid communication state. OPCODE_CONTINUATION unexpected");
} else {
readPayload(frameHeader, nullptr);
}
readPayload(frameHeader, nullptr);
break;

case Frame::OPCODE_TEXT:
Expand Down